#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
Vi v[2][201];
ll dist[2][201],rdist[2][201],tmp[2][201],rtmp[2][201];
int dpre[2][201],rdpre[2][201],tpre[2][201],rtpre[2][201];
bool chk[201],NO[50005];
int x[50001],y[50001];
ll c[50001],d[50001];
int n,m;
void dij(int rev,int S,ll d[2][201],int pre[2][201])
{
fup(i,1,n,1)d[rev][i]=INF,pre[rev][i]=-1,chk[i]=0;
priority_queue<Pll> q;
d[rev][S]=0;
q.push(mp(0,S));
while(!q.empty())
{
int N=q.top().Y;
q.pop();
if(chk[N])continue;
chk[N]=1;
for(int i:v[rev][N])
{
if(NO[i])continue;
int xx=x[i],yy=y[i];
ll cc=c[i];
if(rev)swap(xx,yy);
if(d[rev][yy]>d[rev][N]+cc)
{
d[rev][yy]=d[rev][N]+cc;
pre[rev][yy]=i;
q.push(mp(-d[rev][yy],yy));
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
fup(i,0,m-1,1)
{
scanf("%d%d%lld%lld",x+i,y+i,c+i,d+i);
v[0][x[i]].pb(i);
v[1][y[i]].pb(i);
}
dij(0,1,dist,dpre);dij(0,n,rdist,rdpre);
dij(1,1,dist,dpre);dij(1,n,rdist,rdpre);
ll ans=dist[0][n]+rdist[0][1];
fup(N,1,n,1)
{
for(int i:v[0][N])
{
int xx=x[i],yy=y[i];
ll cc=c[i],dd=d[i];
if(dpre[0][yy]==i || rdpre[0][yy]==i)
{
NO[i]=1;
x[m]=yy,y[m]=xx,c[m]=cc;
v[0][yy].pb(m);
dij(0,1,tmp,tpre);
dij(0,n,rtmp,rtpre);
v[0][yy].pop_back();
NO[i]=0;
ans=min(ans,dd+tmp[0][n]+rtmp[0][1]);
}
else
{
ll tt=dd+min({dist[0][n]+rdist[0][yy]+cc+dist[1][xx], dist[0][yy]+cc+rdist[1][xx]+rdist[0][1], dist[0][yy]+cc+rdist[1][xx]+rdist[0][yy]+cc+dist[1][xx]});
ans=min(ans,tt);
}
}
}
if(ans>=INF)ans=-1;
printf("%lld",ans);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:78:7: 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:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld%lld",x+i,y+i,c+i,d+i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
22 ms |
376 KB |
Output is correct |
4 |
Correct |
24 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
248 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
24 ms |
376 KB |
Output is correct |
11 |
Correct |
29 ms |
376 KB |
Output is correct |
12 |
Correct |
28 ms |
464 KB |
Output is correct |
13 |
Correct |
12 ms |
376 KB |
Output is correct |
14 |
Correct |
16 ms |
376 KB |
Output is correct |
15 |
Correct |
14 ms |
376 KB |
Output is correct |
16 |
Correct |
15 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
2424 KB |
Output is correct |
2 |
Correct |
202 ms |
2296 KB |
Output is correct |
3 |
Correct |
187 ms |
2296 KB |
Output is correct |
4 |
Correct |
21 ms |
376 KB |
Output is correct |
5 |
Correct |
13 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
68 ms |
2168 KB |
Output is correct |
10 |
Correct |
62 ms |
2168 KB |
Output is correct |
11 |
Correct |
131 ms |
2168 KB |
Output is correct |
12 |
Correct |
135 ms |
2332 KB |
Output is correct |
13 |
Correct |
122 ms |
2224 KB |
Output is correct |
14 |
Correct |
107 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
157 ms |
1852 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
172 ms |
2168 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
60 ms |
2168 KB |
Output is correct |
9 |
Correct |
71 ms |
2168 KB |
Output is correct |
10 |
Correct |
121 ms |
2168 KB |
Output is correct |
11 |
Correct |
119 ms |
2172 KB |
Output is correct |
12 |
Correct |
131 ms |
2296 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
376 KB |
Output is correct |
19 |
Correct |
130 ms |
2300 KB |
Output is correct |
20 |
Correct |
119 ms |
2208 KB |
Output is correct |
21 |
Correct |
132 ms |
2168 KB |
Output is correct |
22 |
Correct |
125 ms |
2172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
380 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
22 ms |
376 KB |
Output is correct |
4 |
Correct |
24 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
248 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
24 ms |
376 KB |
Output is correct |
11 |
Correct |
29 ms |
376 KB |
Output is correct |
12 |
Correct |
28 ms |
464 KB |
Output is correct |
13 |
Correct |
12 ms |
376 KB |
Output is correct |
14 |
Correct |
16 ms |
376 KB |
Output is correct |
15 |
Correct |
14 ms |
376 KB |
Output is correct |
16 |
Correct |
15 ms |
376 KB |
Output is correct |
17 |
Correct |
193 ms |
2424 KB |
Output is correct |
18 |
Correct |
202 ms |
2296 KB |
Output is correct |
19 |
Correct |
187 ms |
2296 KB |
Output is correct |
20 |
Correct |
21 ms |
376 KB |
Output is correct |
21 |
Correct |
13 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
68 ms |
2168 KB |
Output is correct |
26 |
Correct |
62 ms |
2168 KB |
Output is correct |
27 |
Correct |
131 ms |
2168 KB |
Output is correct |
28 |
Correct |
135 ms |
2332 KB |
Output is correct |
29 |
Correct |
122 ms |
2224 KB |
Output is correct |
30 |
Correct |
107 ms |
2296 KB |
Output is correct |
31 |
Correct |
15 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
157 ms |
1852 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
172 ms |
2168 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
60 ms |
2168 KB |
Output is correct |
39 |
Correct |
71 ms |
2168 KB |
Output is correct |
40 |
Correct |
121 ms |
2168 KB |
Output is correct |
41 |
Correct |
119 ms |
2172 KB |
Output is correct |
42 |
Correct |
131 ms |
2296 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
376 KB |
Output is correct |
45 |
Correct |
5 ms |
376 KB |
Output is correct |
46 |
Correct |
5 ms |
376 KB |
Output is correct |
47 |
Correct |
5 ms |
376 KB |
Output is correct |
48 |
Correct |
4 ms |
376 KB |
Output is correct |
49 |
Correct |
130 ms |
2300 KB |
Output is correct |
50 |
Correct |
119 ms |
2208 KB |
Output is correct |
51 |
Correct |
132 ms |
2168 KB |
Output is correct |
52 |
Correct |
125 ms |
2172 KB |
Output is correct |
53 |
Correct |
201 ms |
2168 KB |
Output is correct |
54 |
Correct |
236 ms |
2172 KB |
Output is correct |
55 |
Correct |
210 ms |
2168 KB |
Output is correct |
56 |
Correct |
24 ms |
376 KB |
Output is correct |
57 |
Correct |
21 ms |
376 KB |
Output is correct |
58 |
Correct |
134 ms |
1784 KB |
Output is correct |
59 |
Correct |
157 ms |
1784 KB |
Output is correct |
60 |
Correct |
214 ms |
1784 KB |
Output is correct |
61 |
Correct |
142 ms |
1788 KB |
Output is correct |
62 |
Correct |
158 ms |
1920 KB |
Output is correct |
63 |
Correct |
205 ms |
1784 KB |
Output is correct |
64 |
Correct |
791 ms |
2428 KB |
Output is correct |
65 |
Correct |
843 ms |
2640 KB |
Output is correct |
66 |
Execution timed out |
1083 ms |
2544 KB |
Time limit exceeded |
67 |
Halted |
0 ms |
0 KB |
- |