#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define int unsigned
#define pb push_back
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define remin(a,b) a = min(a,b)
typedef map<int,int> mii;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
#define F first
#define S second
#define sfl2(x,y) scanf("%lld%lld",&x,&y);
#define sfi2(x,y) scanf("%d%d",&x,&y);
#define pfi(x) printf("%d\n",x);
#define pfl(x) printf("%lld\n",x);
const int INF=1500000000;
#define runtime() ((double)clock() / CLOCKS_PER_SEC)
void solvethetestcase();
signed main(){
int t = 1;
// (UNCOMMENT FOR MULTIPLE TEST CASES) \
sfl(t);
FOR(testcase,1,t+1){
// (UNCOMMENT FOR CODEJAM) \
printf("Case #%lld: ",testcase);
solvethetestcase();
}
}
int n,m;
vector<pair<pii,pii> > edges;
vi adj[205];
int dist[205][205];
int p1[205],p2[205];
int lol[205];
set<int> s;
int dijkstra(){
REP(i,205) lol[i] = INF;
lol[1] = 0;
priority_queue<pii,vpii,greater<pii> > pq;
pq.push({0,1});
while(pq.size()){
int u = pq.top().S;
pq.pop();
REP(i,adj[u].size()){
int e = adj[u][i];
int v = edges[e].F.S,w = edges[e].S.F;
if(lol[v] > lol[u]+w){
lol[v] = lol[u]+w;
pq.push({lol[v],v});
p1[v] = e;
}
}
}
int res = lol[n];
// if(res >= INF) return INF;
REP(i,205) lol[i] = INF;
lol[n] = 0;
pq.push({0,n});
while(pq.size()){
int u = pq.top().S;
pq.pop();
REP(i,adj[u].size()){
int e = adj[u][i];
int v = edges[e].F.S,w = edges[e].S.F;
if(lol[v] > lol[u]+w){
lol[v] = lol[u]+w;
pq.push({lol[v],v});
p2[v] = e;
}
}
}
return res+lol[1];
}
void solvethetestcase(){
sfi2(n,m)
REP(i,205){
p1[i] = -1;
p2[i] = -1;
REP(j,205){
dist[i][j] = INF;
}
dist[i][i] = 0;
}
REP(i,m){
int u,v,c,d;
sfi2(u,v)
sfi2(c,d)
edges.pb({{u,v},{c,d}});
adj[u].pb(i);
// adj[v].pb(i);
remin(dist[u][v],c);
}
FOR(k,1,n+1){
FOR(i,1,n+1){
FOR(j,1,n+1){
remin(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
int ans = dist[1][n]+dist[n][1];
dijkstra();
// trace(dijkstra(),ans);
FOR(i,1,n+1){
// trace(i,p1[i],p2[i]);
if(p1[i] != -1) s.insert(p1[i]);
if(p2[i] != -1) s.insert(p2[i]);
}
REP(i,m){
int g = edges[i].S.S+min(dist[1][n],dist[1][edges[i].F.S]+dist[edges[i].F.F][n]+edges[i].S.F)+min(dist[n][1],dist[n][edges[i].F.S]+dist[edges[i].F.F][1]+edges[i].S.F);
// if(g >= INF) continue;
if(s.find(i) == s.end()){
// trace(i,edges[i].S.S,dist[1][edges[i].F.S],dist[edges[i].F.F][n],edges[i].S.F);
remin(ans,g);
}
else{
int u,v;
swap(edges[i].F.F,edges[i].F.S);
u = edges[i].F.F,v = edges[i].F.S;
adj[u].pb(i);
REP(j,adj[v].size()){
if(adj[v][j] == i){
adj[v].erase(adj[v].begin()+j);
}
}
// trace(i,dijkstra(),edges[i].S.S);
remin(ans,dijkstra()+edges[i].S.S);
swap(edges[i].F.F,edges[i].F.S);
u = edges[i].F.F,v = edges[i].F.S;
adj[u].pb(i);
REP(j,adj[v].size()){
if(adj[v][j] == i){
adj[v].erase(adj[v].begin()+j);
}
}
}
if(runtime() > 0.98){
break;
}
}
if(ans >= INF) pfi(-1)
else pfi(ans)
// cout << fixed << setprecision(10) << runtime() << endl;
}
Compilation message
ho_t4.cpp:59:2: warning: multi-line comment [-Wcomment]
// (UNCOMMENT FOR MULTIPLE TEST CASES) \
^
ho_t4.cpp:62:3: warning: multi-line comment [-Wcomment]
// (UNCOMMENT FOR CODEJAM) \
^
ho_t4.cpp: In function 'void solvethetestcase()':
ho_t4.cpp:148:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p1[i] != -1) s.insert(p1[i]);
~~~~~~^~~~~
ho_t4.cpp:149:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(p2[i] != -1) s.insert(p2[i]);
~~~~~~^~~~~
ho_t4.cpp:46:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sfi2(x,y) scanf("%d%d",&x,&y);
~~~~~^~~~~~~~~~~~~~
ho_t4.cpp:117:2: note: in expansion of macro 'sfi2'
sfi2(n,m)
^~~~
ho_t4.cpp:46:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sfi2(x,y) scanf("%d%d",&x,&y);
~~~~~^~~~~~~~~~~~~~
ho_t4.cpp:128:3: note: in expansion of macro 'sfi2'
sfi2(u,v)
^~~~
ho_t4.cpp:46:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sfi2(x,y) scanf("%d%d",&x,&y);
~~~~~^~~~~~~~~~~~~~
ho_t4.cpp:129:3: note: in expansion of macro 'sfi2'
sfi2(c,d)
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
512 KB |
Output is correct |
2 |
Correct |
14 ms |
512 KB |
Output is correct |
3 |
Correct |
34 ms |
512 KB |
Output is correct |
4 |
Correct |
34 ms |
512 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
14 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
34 ms |
512 KB |
Output is correct |
11 |
Correct |
40 ms |
512 KB |
Output is correct |
12 |
Correct |
39 ms |
512 KB |
Output is correct |
13 |
Correct |
18 ms |
512 KB |
Output is correct |
14 |
Correct |
26 ms |
512 KB |
Output is correct |
15 |
Correct |
24 ms |
512 KB |
Output is correct |
16 |
Correct |
24 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
491 ms |
2680 KB |
Output is correct |
2 |
Correct |
502 ms |
2680 KB |
Output is correct |
3 |
Correct |
513 ms |
2808 KB |
Output is correct |
4 |
Correct |
31 ms |
640 KB |
Output is correct |
5 |
Correct |
21 ms |
512 KB |
Output is correct |
6 |
Correct |
14 ms |
512 KB |
Output is correct |
7 |
Correct |
14 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
137 ms |
2796 KB |
Output is correct |
10 |
Correct |
131 ms |
2796 KB |
Output is correct |
11 |
Correct |
343 ms |
2800 KB |
Output is correct |
12 |
Correct |
340 ms |
2940 KB |
Output is correct |
13 |
Correct |
322 ms |
2924 KB |
Output is correct |
14 |
Correct |
252 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
512 KB |
Output is correct |
2 |
Correct |
14 ms |
512 KB |
Output is correct |
3 |
Correct |
166 ms |
2552 KB |
Output is correct |
4 |
Correct |
14 ms |
512 KB |
Output is correct |
5 |
Correct |
178 ms |
2552 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
84 ms |
2540 KB |
Output is correct |
9 |
Correct |
97 ms |
2668 KB |
Output is correct |
10 |
Correct |
136 ms |
2552 KB |
Output is correct |
11 |
Correct |
126 ms |
2552 KB |
Output is correct |
12 |
Correct |
143 ms |
2544 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
139 ms |
2584 KB |
Output is correct |
20 |
Correct |
130 ms |
2552 KB |
Output is correct |
21 |
Correct |
153 ms |
2552 KB |
Output is correct |
22 |
Correct |
131 ms |
2544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
512 KB |
Output is correct |
2 |
Correct |
14 ms |
512 KB |
Output is correct |
3 |
Correct |
34 ms |
512 KB |
Output is correct |
4 |
Correct |
34 ms |
512 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
14 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
34 ms |
512 KB |
Output is correct |
11 |
Correct |
40 ms |
512 KB |
Output is correct |
12 |
Correct |
39 ms |
512 KB |
Output is correct |
13 |
Correct |
18 ms |
512 KB |
Output is correct |
14 |
Correct |
26 ms |
512 KB |
Output is correct |
15 |
Correct |
24 ms |
512 KB |
Output is correct |
16 |
Correct |
24 ms |
512 KB |
Output is correct |
17 |
Correct |
491 ms |
2680 KB |
Output is correct |
18 |
Correct |
502 ms |
2680 KB |
Output is correct |
19 |
Correct |
513 ms |
2808 KB |
Output is correct |
20 |
Correct |
31 ms |
640 KB |
Output is correct |
21 |
Correct |
21 ms |
512 KB |
Output is correct |
22 |
Correct |
14 ms |
512 KB |
Output is correct |
23 |
Correct |
14 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
137 ms |
2796 KB |
Output is correct |
26 |
Correct |
131 ms |
2796 KB |
Output is correct |
27 |
Correct |
343 ms |
2800 KB |
Output is correct |
28 |
Correct |
340 ms |
2940 KB |
Output is correct |
29 |
Correct |
322 ms |
2924 KB |
Output is correct |
30 |
Correct |
252 ms |
2796 KB |
Output is correct |
31 |
Correct |
23 ms |
512 KB |
Output is correct |
32 |
Correct |
14 ms |
512 KB |
Output is correct |
33 |
Correct |
166 ms |
2552 KB |
Output is correct |
34 |
Correct |
14 ms |
512 KB |
Output is correct |
35 |
Correct |
178 ms |
2552 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
5 ms |
512 KB |
Output is correct |
38 |
Correct |
84 ms |
2540 KB |
Output is correct |
39 |
Correct |
97 ms |
2668 KB |
Output is correct |
40 |
Correct |
136 ms |
2552 KB |
Output is correct |
41 |
Correct |
126 ms |
2552 KB |
Output is correct |
42 |
Correct |
143 ms |
2544 KB |
Output is correct |
43 |
Correct |
5 ms |
512 KB |
Output is correct |
44 |
Correct |
5 ms |
512 KB |
Output is correct |
45 |
Correct |
5 ms |
512 KB |
Output is correct |
46 |
Correct |
5 ms |
512 KB |
Output is correct |
47 |
Correct |
5 ms |
512 KB |
Output is correct |
48 |
Correct |
5 ms |
512 KB |
Output is correct |
49 |
Correct |
139 ms |
2584 KB |
Output is correct |
50 |
Correct |
130 ms |
2552 KB |
Output is correct |
51 |
Correct |
153 ms |
2552 KB |
Output is correct |
52 |
Correct |
131 ms |
2544 KB |
Output is correct |
53 |
Correct |
578 ms |
2680 KB |
Output is correct |
54 |
Correct |
576 ms |
2680 KB |
Output is correct |
55 |
Correct |
554 ms |
2680 KB |
Output is correct |
56 |
Correct |
34 ms |
512 KB |
Output is correct |
57 |
Correct |
31 ms |
512 KB |
Output is correct |
58 |
Correct |
335 ms |
2552 KB |
Output is correct |
59 |
Correct |
392 ms |
2552 KB |
Output is correct |
60 |
Correct |
593 ms |
2544 KB |
Output is correct |
61 |
Correct |
317 ms |
2552 KB |
Output is correct |
62 |
Correct |
381 ms |
2552 KB |
Output is correct |
63 |
Correct |
551 ms |
2552 KB |
Output is correct |
64 |
Correct |
989 ms |
2544 KB |
Output is correct |
65 |
Correct |
989 ms |
2544 KB |
Output is correct |
66 |
Correct |
984 ms |
2544 KB |
Output is correct |
67 |
Correct |
44 ms |
2552 KB |
Output is correct |
68 |
Correct |
152 ms |
2796 KB |
Output is correct |
69 |
Correct |
142 ms |
2800 KB |
Output is correct |
70 |
Correct |
401 ms |
2800 KB |
Output is correct |
71 |
Correct |
413 ms |
2808 KB |
Output is correct |
72 |
Correct |
375 ms |
2868 KB |
Output is correct |
73 |
Correct |
424 ms |
2936 KB |
Output is correct |
74 |
Correct |
370 ms |
2800 KB |
Output is correct |