#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
vector<pair<int,llo>> adj[201];
vector<llo> cost[201];
llo dis[201][201];
int yes[201][201];
map<pair<int,llo>,llo> dob;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llo n,m;
memset(dis,-1,sizeof(dis));
memset(yes,0,sizeof(yes));
cin>>n>>m;
llo cc,dd;
int ac,bb;
for(llo i=0;i<m;i++){
cin>>ac>>bb>>cc>>dd;
adj[ac-1].pb({bb-1,cc});
cost[ac-1].pb(dd);
if(dis[ac-1][bb-1]==-1){
dis[ac-1][bb-1]=cc;
}
dis[ac-1][bb-1]=min(dis[ac-1][bb-1],cc);
}
for(llo i=0;i<n;i++){
dis[i][i]=0;
}
for(llo k=0;k<n;k++){
for(llo i=0;i<n;i++){
for(llo j=0;j<n;j++){
if(dis[i][k]!=-1 and dis[k][j]!=-1){
if(dis[i][j]==-1){
dis[i][j]=dis[i][k]+dis[k][j];
}
else{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
}
llo ans=-1;
if(dis[0][n-1]>-1 and dis[n-1][0]>-1){
ans=dis[0][n-1]+dis[n-1][0];
}
priority_queue<pair<llo,int>> cd;
llo dist3[n];
pair<int,llo> par[n];
for(llo i=0;i<n;i++){
dist3[i]=-1;
}
dist3[0]=0;
cd.push({0,0});
while(cd.size()){
pair<llo,int> bb=cd.top();
cd.pop();
bb.a=-bb.a;
for(int k=0;k<adj[bb.b].size();k++){
pair<int,int> jj=adj[bb.b][k];
if(dist3[jj.a]==-1 or dist3[jj.a]>bb.a+jj.b ){
dist3[jj.a]=bb.a+jj.b;
cd.push({-dist3[jj.a],jj.a});
par[jj.a]={bb.b,k};
}
}
}
llo tot5=0;
if(dist3[n-1]>-1){
llo nn=n-1;
while(nn!=0){
yes[par[nn].a][par[nn].b]=1;
nn=par[nn].a;
tot5+=1;
}
}
pair<int,llo> par2[n];
llo dist4[n];
for(llo i=0;i<n;i++){
dist4[i]=-1;
}
dist4[n-1]=0;
cd.push({0,n-1});
while(cd.size()){
pair<llo,int> bb=cd.top();
cd.pop();
bb.a=-bb.a;
for(int k=0;k<adj[bb.b].size();k++){
pair<int,int> jj=adj[bb.b][k];
if(dist4[jj.a]==-1 or dist4[jj.a]>bb.a+jj.b ){
dist4[jj.a]=bb.a+jj.b;
cd.push({-dist4[jj.a],jj.a});
par[jj.a]={bb.b,k};
}
}
}
if(dist4[0]>-1){
llo nn=0;
while(nn!=n-1){
yes[par[nn].a][par[nn].b]=1;
nn=par[nn].a;
tot5+=1;
}
}
for(llo i=0;i<n;i++){
for(llo j=0;j<adj[i].size();j++){
llo nn=adj[i][j].a;
if(yes[i][j]==0){
if(dis[i][0]>-1 and dis[0][n-1]>-1 and dis[n-1][nn]>-1){
if(ans==-1){
ans=dis[i][0]+cost[i][j]+dis[n-1][nn]+dis[0][n-1]+adj[i][j].b;
}
ans=min(ans,dis[i][0]+cost[i][j]+dis[n-1][nn]+dis[0][n-1]+adj[i][j].b);
}
if(dis[i][n-1]>-1 and dis[n-1][0]>-1 and dis[0][nn]>-1){
if(ans==-1){
ans=dis[i][n-1]+cost[i][j]+dis[0][nn]+dis[n-1][0]+adj[i][j].b;
}
ans=min(ans,dis[i][n-1]+cost[i][j]+dis[0][nn]+dis[n-1][0]+adj[i][j].b);
}
if(dis[0][nn]>-1 and dis[n-1][nn]>-1 and dis[i][0]>-1 and dis[i][n-1]>0){
if(ans==-1){
ans=dis[0][nn]+dis[n-1][nn]+dis[i][0]+dis[i][n-1]+adj[i][j].b*2+cost[i][j];
}
ans=min(ans,dis[0][nn]+dis[n-1][nn]+dis[i][0]+dis[i][n-1]+adj[i][j].b*2+cost[i][j]);
}
}
else{
priority_queue<pair<llo,int>> cc;
llo dist[n];
for(llo i=0;i<n;i++){
dist[i]=-1;
}
dist[0]=0;
cc.push({0,0});
while(cc.size()){
pair<llo,int> bb=cc.top();
cc.pop();
bb.a=-bb.a;
for(auto jj:adj[bb.b]){
if(bb.b==i and jj.a==nn){
continue;
}
if(dist[jj.a]==-1 or dist[jj.a]>bb.a+jj.b ){
dist[jj.a]=bb.a+jj.b;
cc.push({-dist[jj.a],jj.a});
}
}
if(bb.b==nn){
if(dist[i]==-1 or dist[i]>dist[bb.b]+adj[i][j].b){
dist[i]=dist[bb.b]+adj[i][j].b;
cc.push({-dist[i],i});
}
}
}
llo dist2[n];
for(llo i=0;i<n;i++){
dist2[i]=-1;
}
dist2[n-1]=0;
cc.push({0,n-1});
while(cc.size()){
pair<llo,int> bb=cc.top();
cc.pop();
bb.a=-bb.a;
for(auto jj:adj[bb.b]){
if(bb.b==i and jj.a==nn){
continue;
}
if(dist2[jj.a]==-1 or dist2[jj.a]>bb.a+jj.b ){
dist2[jj.a]=bb.a+jj.b;
cc.push({-dist2[jj.a],jj.a});
}
}
if(bb.b==nn){
if(dist2[i]==-1 or dist2[i]>dist2[bb.b]+adj[i][j].b){
dist2[i]=dist2[bb.a]+adj[i][j].b;
cc.push({-dist2[i],i});
}
}
}
if(dist[n-1]>-1 and dist2[0]>-1){
if(ans==-1){
ans=dist[n-1]+dist2[0]+cost[i][j];
}
ans=min(ans,dist[n-1]+dist2[0]+cost[i][j]);
}
}
}
}
cout<<ans<<endl;
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:69:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<adj[bb.b].size();k++){
~^~~~~~~~~~~~~~~~~
ho_t4.cpp:102:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=0;k<adj[bb.b].size();k++){
~^~~~~~~~~~~~~~~~~
ho_t4.cpp:121:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(llo j=0;j<adj[i].size();j++){
~^~~~~~~~~~~~~~
ho_t4.cpp:87:16: warning: unused variable 'par2' [-Wunused-variable]
pair<int,llo> par2[n];
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
785 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
47 ms |
5268 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
874 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
785 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |