#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 |
12 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
9 ms |
856 KB |
Output is correct |
4 |
Correct |
10 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
15 ms |
860 KB |
Output is correct |
11 |
Correct |
40 ms |
856 KB |
Output is correct |
12 |
Correct |
33 ms |
856 KB |
Output is correct |
13 |
Correct |
8 ms |
856 KB |
Output is correct |
14 |
Correct |
9 ms |
856 KB |
Output is correct |
15 |
Correct |
9 ms |
856 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
6592 KB |
Output is correct |
2 |
Correct |
44 ms |
6344 KB |
Output is correct |
3 |
Correct |
43 ms |
6592 KB |
Output is correct |
4 |
Correct |
10 ms |
856 KB |
Output is correct |
5 |
Correct |
10 ms |
856 KB |
Output is correct |
6 |
Correct |
8 ms |
856 KB |
Output is correct |
7 |
Correct |
7 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
29 ms |
6592 KB |
Output is correct |
10 |
Correct |
30 ms |
6596 KB |
Output is correct |
11 |
Correct |
44 ms |
6592 KB |
Output is correct |
12 |
Correct |
41 ms |
6596 KB |
Output is correct |
13 |
Correct |
41 ms |
6592 KB |
Output is correct |
14 |
Correct |
40 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
41 ms |
5712 KB |
Output is correct |
4 |
Correct |
7 ms |
600 KB |
Output is correct |
5 |
Correct |
37 ms |
6364 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
29 ms |
6344 KB |
Output is correct |
9 |
Correct |
31 ms |
6336 KB |
Output is correct |
10 |
Correct |
35 ms |
6348 KB |
Output is correct |
11 |
Correct |
35 ms |
6180 KB |
Output is correct |
12 |
Correct |
35 ms |
6332 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
856 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
36 ms |
6348 KB |
Output is correct |
20 |
Correct |
37 ms |
6336 KB |
Output is correct |
21 |
Correct |
39 ms |
6344 KB |
Output is correct |
22 |
Correct |
36 ms |
6336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
856 KB |
Output is correct |
2 |
Correct |
7 ms |
600 KB |
Output is correct |
3 |
Correct |
9 ms |
856 KB |
Output is correct |
4 |
Correct |
10 ms |
856 KB |
Output is correct |
5 |
Correct |
1 ms |
856 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
856 KB |
Output is correct |
10 |
Correct |
15 ms |
860 KB |
Output is correct |
11 |
Correct |
40 ms |
856 KB |
Output is correct |
12 |
Correct |
33 ms |
856 KB |
Output is correct |
13 |
Correct |
8 ms |
856 KB |
Output is correct |
14 |
Correct |
9 ms |
856 KB |
Output is correct |
15 |
Correct |
9 ms |
856 KB |
Output is correct |
16 |
Correct |
9 ms |
860 KB |
Output is correct |
17 |
Correct |
50 ms |
6592 KB |
Output is correct |
18 |
Correct |
44 ms |
6344 KB |
Output is correct |
19 |
Correct |
43 ms |
6592 KB |
Output is correct |
20 |
Correct |
10 ms |
856 KB |
Output is correct |
21 |
Correct |
10 ms |
856 KB |
Output is correct |
22 |
Correct |
8 ms |
856 KB |
Output is correct |
23 |
Correct |
7 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
29 ms |
6592 KB |
Output is correct |
26 |
Correct |
30 ms |
6596 KB |
Output is correct |
27 |
Correct |
44 ms |
6592 KB |
Output is correct |
28 |
Correct |
41 ms |
6596 KB |
Output is correct |
29 |
Correct |
41 ms |
6592 KB |
Output is correct |
30 |
Correct |
40 ms |
6604 KB |
Output is correct |
31 |
Correct |
12 ms |
856 KB |
Output is correct |
32 |
Correct |
7 ms |
600 KB |
Output is correct |
33 |
Correct |
41 ms |
5712 KB |
Output is correct |
34 |
Correct |
7 ms |
600 KB |
Output is correct |
35 |
Correct |
37 ms |
6364 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
29 ms |
6344 KB |
Output is correct |
39 |
Correct |
31 ms |
6336 KB |
Output is correct |
40 |
Correct |
35 ms |
6348 KB |
Output is correct |
41 |
Correct |
35 ms |
6180 KB |
Output is correct |
42 |
Correct |
35 ms |
6332 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
600 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
0 ms |
604 KB |
Output is correct |
47 |
Correct |
0 ms |
856 KB |
Output is correct |
48 |
Correct |
1 ms |
600 KB |
Output is correct |
49 |
Correct |
36 ms |
6348 KB |
Output is correct |
50 |
Correct |
37 ms |
6336 KB |
Output is correct |
51 |
Correct |
39 ms |
6344 KB |
Output is correct |
52 |
Correct |
36 ms |
6336 KB |
Output is correct |
53 |
Correct |
47 ms |
6336 KB |
Output is correct |
54 |
Correct |
59 ms |
6344 KB |
Output is correct |
55 |
Correct |
55 ms |
6336 KB |
Output is correct |
56 |
Correct |
9 ms |
856 KB |
Output is correct |
57 |
Correct |
9 ms |
856 KB |
Output is correct |
58 |
Correct |
58 ms |
5704 KB |
Output is correct |
59 |
Correct |
160 ms |
5572 KB |
Output is correct |
60 |
Correct |
446 ms |
5580 KB |
Output is correct |
61 |
Correct |
174 ms |
5580 KB |
Output is correct |
62 |
Correct |
162 ms |
5572 KB |
Output is correct |
63 |
Correct |
446 ms |
5580 KB |
Output is correct |
64 |
Correct |
61 ms |
5580 KB |
Output is correct |
65 |
Correct |
150 ms |
5580 KB |
Output is correct |
66 |
Correct |
426 ms |
5580 KB |
Output is correct |
67 |
Correct |
22 ms |
5580 KB |
Output is correct |
68 |
Correct |
32 ms |
6536 KB |
Output is correct |
69 |
Correct |
30 ms |
6572 KB |
Output is correct |
70 |
Correct |
49 ms |
6604 KB |
Output is correct |
71 |
Correct |
45 ms |
6592 KB |
Output is correct |
72 |
Correct |
44 ms |
6632 KB |
Output is correct |
73 |
Correct |
45 ms |
6600 KB |
Output is correct |
74 |
Correct |
48 ms |
6592 KB |
Output is correct |