#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef pair<ii,ii> i4;
i4 G[50005];
vector<ii> adjlist[205];
int ans = 2000000000;
int n,m;
int dist[205];
priority_queue<ii,vector<ii>, greater<ii> > pq;
int test(){
for (int i = 1; i <= n; i++) adjlist[i].clear();
for (int i = 0; i < m; i++){
int u = G[i].first.first;
int v = G[i].first.second;
int c = G[i].second.first;
adjlist[u].push_back({v,c});
}
memset(dist,-1,sizeof(dist));
dist[1] = 0;
pq.push({0,1});
while (pq.size()){
ii cur = pq.top(); pq.pop();
int d = cur.first, u = cur.second;
if (dist[u] < d) continue;
for (auto v: adjlist[u]){
int newdist = dist[u]+v.second;
if (dist[v.first] == -1 || newdist < dist[v.first]){
dist[v.first] = newdist;
pq.push({dist[v.first],v.first});
}
}
}
int L = dist[n];
memset(dist,-1,sizeof(dist));
dist[n] = 0;
pq.push({0,n});
while (pq.size()){
ii cur = pq.top(); pq.pop();
int d = cur.first, u = cur.second;
if (dist[u] < d) continue;
for (auto v: adjlist[u]){
int newdist = dist[u]+v.second;
if (dist[v.first] == -1 || newdist < dist[v.first]){
dist[v.first] = newdist;
pq.push({dist[v.first],v.first});
}
}
}
int R = dist[1];
if (L == -1 || R == -1) return -1;
else return L+R;
}
int main(){
scanf("%d%d",&n,&m);
for (int i = 0; i < m; i++){
int u,v,c,d;
scanf("%d%d%d%d",&u,&v,&c,&d);
G[i].first.first = u;
G[i].first.second = v;
G[i].second.first = c;
G[i].second.second = d;
}
int x = test();
//printf("noflip, %d\n",x);
if (x != -1) ans = min(ans,x);
for (int i = 0; i< m; i++){
int cost = G[i].second.second;
swap(G[i].first.first,G[i].first.second);
int r = test();
swap(G[i].first.first,G[i].first.second);
//printf("%d %d\n",cost,r);
if (r == -1) continue;
ans = min(ans,cost+r);
}
if (ans == 2000000000){
printf("-1");
return 0;
}
else printf("%d \n",ans);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
ho_t4.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d",&u,&v,&c,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
63 ms |
504 KB |
Output is correct |
4 |
Correct |
67 ms |
384 KB |
Output is correct |
5 |
Correct |
27 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
18 ms |
512 KB |
Output is correct |
10 |
Correct |
89 ms |
384 KB |
Output is correct |
11 |
Correct |
87 ms |
384 KB |
Output is correct |
12 |
Correct |
88 ms |
384 KB |
Output is correct |
13 |
Correct |
39 ms |
384 KB |
Output is correct |
14 |
Correct |
49 ms |
384 KB |
Output is correct |
15 |
Correct |
45 ms |
384 KB |
Output is correct |
16 |
Correct |
46 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1084 ms |
1796 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Execution timed out |
1083 ms |
1408 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
63 ms |
504 KB |
Output is correct |
4 |
Correct |
67 ms |
384 KB |
Output is correct |
5 |
Correct |
27 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
18 ms |
512 KB |
Output is correct |
10 |
Correct |
89 ms |
384 KB |
Output is correct |
11 |
Correct |
87 ms |
384 KB |
Output is correct |
12 |
Correct |
88 ms |
384 KB |
Output is correct |
13 |
Correct |
39 ms |
384 KB |
Output is correct |
14 |
Correct |
49 ms |
384 KB |
Output is correct |
15 |
Correct |
45 ms |
384 KB |
Output is correct |
16 |
Correct |
46 ms |
384 KB |
Output is correct |
17 |
Execution timed out |
1084 ms |
1796 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |