#include "bits/stdc++.h"
#pragma optimize ("Bismillahirrahmanirrahim")
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define endl "\n"
#define int long long
#define double long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define what_is(x) cerr << #x << " is " << x << endl;
//#define m (l+r)/2
constexpr int N=200005;
constexpr int MOD=1000000007;
constexpr int INF2 = LLONG_MAX;
constexpr int INF=(int)1e15;
constexpr int LOG=30;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
typedef priority_queue<pii,vector<pii>,greater<pii>> min_pq;
typedef priority_queue<pii> max_pq;
typedef long long ll;
//to think//
/*
* graph approach
* dp
* dividing the problem to smaller statements
* finding the real constraint
* sqrt decomposition
* greedy approach
* pigeonhole principle
* rewriting the problem/equality
* bitwise approaches
* binary search if monotonic
* divide and conquer
* combinatorics
* inclusion - exclusion
* think like bfs
*/
inline int in()
{
int x;cin >> x;
return x;
}
inline string in2()
{
string x;cin >> x;
return x;
}
/*
1- sp route cikar (n^2 dijkstra ile)
2- sp routetaki her edge için tekrardan dijkstra(n^2) at
3- sp routetaki edge değilse dist[1][b] + c + d dist[a][n] dene
*/
int n,m;
multiset<array<int,4>> git;
vector<array<int,4>> edge;
multiset<array<int,3>> v[205];
int dijk(int rt)
{
int di[n+2];
bool vis[n+2];
for(int i=1;i<=n;i++) {di[i]=INF;vis[i]=0;}
di[rt]=0;
for(int i=1;i<=n;i++)
{
pii cur={INF,INF};
for(int j=1;j<=n;j++) if(vis[j]==0) cur=min(cur,{di[j],j});
if(cur.ff==INF) break;
vis[cur.ss]=1;
for(array<int,3> x:v[cur.ss]) if(cur.ff+x[1]<di[x[0]]) di[x[0]]=cur.ff+x[1];
}
if(rt==1) return di[n];
else return di[1];
}
void rout(int rt)
{
int di[n+1];
bool vis[n+1];
for(int i=1;i<=n;i++) {di[i]=INF;vis[i]=0;}
di[rt]=0;
array<int,3> par[n+1];
for(int i=1;i<=n;i++)
{
pii cur={INF,INF};
for(int j=1;j<=n;j++) if(vis[j]==0) cur=min(cur,{di[j],j});
if(cur.ff==INF) break;
vis[cur.ss]=1;
for(array<int,3> x:v[cur.ss])
if(cur.ff+x[1]<di[x[0]])
{par[x[0]]={cur.ss,x[1],x[2]};di[x[0]]=cur.ff+x[1];}
}
if(rt==1)
{
if(vis[n]==0) return;
int xd=n;
while(xd!=1)
{
git.insert({par[xd][0],xd,par[xd][1],par[xd][2]});
xd=par[xd][0];
}
}
else
{
int xd=1;
if(vis[1]==0) return;
while(xd!=n)
{
git.insert({par[xd][0],xd,par[xd][1],par[xd][2]});
xd=par[xd][0];
}
}
}
int dist[202][202];
void solve()
{
n=in(),m=in();
for(int i=1;i<=201;i++) for(int j=1;j<=201;j++) dist[i][j]=INF;
for(int i=1;i<=201;i++) dist[i][i]=0;
for(int i=1;i<=m;i++)
{
int a=in(),b=in(),c=in(),d=in();
edge.pb({a,b,c,d});
dist[a][b]=min(dist[a][b],c);
v[a].insert({b,c,d});
}
rout(1);
rout(n);
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
int ans=dist[1][n]+dist[n][1];
for(array<int,4> x:edge)
{
int gitme=INF;
int gelme=INF;
if(git.count(x)==1)
{
v[x[0]].erase(v[x[0]].find({x[1],x[2],x[3]}));
v[x[1]].insert({x[0],x[2],x[3]});
gitme=min(gitme,dijk(1));
gelme=min(gelme,dijk(n));
v[x[1]].erase(v[x[1]].find({x[0],x[2],x[3]}));
v[x[0]].insert({x[1],x[2],x[3]});
ans=min(ans,gitme+gelme+x[3]);
}
else
{
gitme=min(gitme,dist[1][n]);
gitme=min(gitme,dist[1][x[1]]+dist[x[0]][n]+x[2]);
gelme=min(gelme,dist[n][1]);
gelme=min(gelme,dist[n][x[1]]+x[2]+dist[x[0]][1]);
ans=min(ans,gitme+gelme+x[3]);
}
}
cout << (ans>=INF ? -1 :ans) << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
cout << fixed << setprecision(15);
int t=1;//t=in();
for(int i=1;i<=t;i++)
{
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
Compilation message
ho_t4.cpp:2: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
2 | #pragma optimize ("Bismillahirrahmanirrahim")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
756 KB |
Output is correct |
2 |
Correct |
8 ms |
680 KB |
Output is correct |
3 |
Correct |
10 ms |
748 KB |
Output is correct |
4 |
Correct |
12 ms |
752 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
21 ms |
724 KB |
Output is correct |
11 |
Correct |
46 ms |
724 KB |
Output is correct |
12 |
Correct |
38 ms |
724 KB |
Output is correct |
13 |
Correct |
8 ms |
752 KB |
Output is correct |
14 |
Correct |
10 ms |
724 KB |
Output is correct |
15 |
Correct |
9 ms |
724 KB |
Output is correct |
16 |
Correct |
10 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
5248 KB |
Output is correct |
2 |
Correct |
54 ms |
5360 KB |
Output is correct |
3 |
Correct |
53 ms |
5264 KB |
Output is correct |
4 |
Correct |
12 ms |
724 KB |
Output is correct |
5 |
Correct |
12 ms |
724 KB |
Output is correct |
6 |
Correct |
8 ms |
728 KB |
Output is correct |
7 |
Correct |
8 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
596 KB |
Output is correct |
9 |
Correct |
34 ms |
5264 KB |
Output is correct |
10 |
Correct |
34 ms |
5256 KB |
Output is correct |
11 |
Correct |
56 ms |
5356 KB |
Output is correct |
12 |
Correct |
67 ms |
5240 KB |
Output is correct |
13 |
Correct |
49 ms |
5328 KB |
Output is correct |
14 |
Correct |
49 ms |
5316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
748 KB |
Output is correct |
2 |
Correct |
8 ms |
676 KB |
Output is correct |
3 |
Correct |
39 ms |
4740 KB |
Output is correct |
4 |
Correct |
9 ms |
676 KB |
Output is correct |
5 |
Correct |
42 ms |
5316 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
38 ms |
5324 KB |
Output is correct |
9 |
Correct |
39 ms |
5300 KB |
Output is correct |
10 |
Correct |
54 ms |
5292 KB |
Output is correct |
11 |
Correct |
41 ms |
5240 KB |
Output is correct |
12 |
Correct |
40 ms |
5316 KB |
Output is correct |
13 |
Correct |
0 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
628 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
46 ms |
5364 KB |
Output is correct |
20 |
Correct |
47 ms |
5328 KB |
Output is correct |
21 |
Correct |
67 ms |
5344 KB |
Output is correct |
22 |
Correct |
42 ms |
5280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
756 KB |
Output is correct |
2 |
Correct |
8 ms |
680 KB |
Output is correct |
3 |
Correct |
10 ms |
748 KB |
Output is correct |
4 |
Correct |
12 ms |
752 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
724 KB |
Output is correct |
10 |
Correct |
21 ms |
724 KB |
Output is correct |
11 |
Correct |
46 ms |
724 KB |
Output is correct |
12 |
Correct |
38 ms |
724 KB |
Output is correct |
13 |
Correct |
8 ms |
752 KB |
Output is correct |
14 |
Correct |
10 ms |
724 KB |
Output is correct |
15 |
Correct |
9 ms |
724 KB |
Output is correct |
16 |
Correct |
10 ms |
724 KB |
Output is correct |
17 |
Correct |
48 ms |
5248 KB |
Output is correct |
18 |
Correct |
54 ms |
5360 KB |
Output is correct |
19 |
Correct |
53 ms |
5264 KB |
Output is correct |
20 |
Correct |
12 ms |
724 KB |
Output is correct |
21 |
Correct |
12 ms |
724 KB |
Output is correct |
22 |
Correct |
8 ms |
728 KB |
Output is correct |
23 |
Correct |
8 ms |
596 KB |
Output is correct |
24 |
Correct |
0 ms |
596 KB |
Output is correct |
25 |
Correct |
34 ms |
5264 KB |
Output is correct |
26 |
Correct |
34 ms |
5256 KB |
Output is correct |
27 |
Correct |
56 ms |
5356 KB |
Output is correct |
28 |
Correct |
67 ms |
5240 KB |
Output is correct |
29 |
Correct |
49 ms |
5328 KB |
Output is correct |
30 |
Correct |
49 ms |
5316 KB |
Output is correct |
31 |
Correct |
13 ms |
748 KB |
Output is correct |
32 |
Correct |
8 ms |
676 KB |
Output is correct |
33 |
Correct |
39 ms |
4740 KB |
Output is correct |
34 |
Correct |
9 ms |
676 KB |
Output is correct |
35 |
Correct |
42 ms |
5316 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
38 ms |
5324 KB |
Output is correct |
39 |
Correct |
39 ms |
5300 KB |
Output is correct |
40 |
Correct |
54 ms |
5292 KB |
Output is correct |
41 |
Correct |
41 ms |
5240 KB |
Output is correct |
42 |
Correct |
40 ms |
5316 KB |
Output is correct |
43 |
Correct |
0 ms |
596 KB |
Output is correct |
44 |
Correct |
0 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
628 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
46 ms |
5364 KB |
Output is correct |
50 |
Correct |
47 ms |
5328 KB |
Output is correct |
51 |
Correct |
67 ms |
5344 KB |
Output is correct |
52 |
Correct |
42 ms |
5280 KB |
Output is correct |
53 |
Correct |
52 ms |
5316 KB |
Output is correct |
54 |
Correct |
70 ms |
5240 KB |
Output is correct |
55 |
Correct |
61 ms |
5280 KB |
Output is correct |
56 |
Correct |
10 ms |
728 KB |
Output is correct |
57 |
Correct |
13 ms |
724 KB |
Output is correct |
58 |
Correct |
68 ms |
4680 KB |
Output is correct |
59 |
Correct |
183 ms |
4680 KB |
Output is correct |
60 |
Correct |
545 ms |
4676 KB |
Output is correct |
61 |
Correct |
200 ms |
4680 KB |
Output is correct |
62 |
Correct |
210 ms |
4776 KB |
Output is correct |
63 |
Correct |
493 ms |
4704 KB |
Output is correct |
64 |
Correct |
72 ms |
4680 KB |
Output is correct |
65 |
Correct |
168 ms |
4756 KB |
Output is correct |
66 |
Correct |
488 ms |
4680 KB |
Output is correct |
67 |
Correct |
30 ms |
4680 KB |
Output is correct |
68 |
Correct |
39 ms |
5256 KB |
Output is correct |
69 |
Correct |
43 ms |
5324 KB |
Output is correct |
70 |
Correct |
58 ms |
5304 KB |
Output is correct |
71 |
Correct |
52 ms |
5308 KB |
Output is correct |
72 |
Correct |
49 ms |
5328 KB |
Output is correct |
73 |
Correct |
78 ms |
5344 KB |
Output is correct |
74 |
Correct |
63 ms |
5272 KB |
Output is correct |