This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
// #define int long long
#define ll long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "robot"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<ll,ll> ii;
typedef pair<ll,ii> iii;
typedef vector<ll> vi;
const int MAXN = 1e5 + 9;
unordered_map<int,vector<iii>> g[MAXN];
unordered_map<int,ll> psum[MAXN];
ll dist[MAXN],n,m;
unordered_map<int,ll> dp[MAXN];
//dp[i]: khoang cach ngan nhat den dinh i.
//dp2[i][c]: khoang cach ngan nhat den i sao cho:
/*
- Ta di qua mot canh co mau c.
- Ta chua to mau lai canh do.
- Ta se roi dinh i de di den 1 dinh khac co mau c va ta se to mau lai cac neighbor cua no.
*/
main()
{
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v,w,c,p;
cin>>u>>v>>c>>p;
g[u][c].pb({v,{c,p}});
g[v][c].pb({u,{c,p}});
psum[u][c] += p;
psum[v][c] += p;
}
priority_queue<tuple<ll,int,int>> pq;
memset(dist,0x3f,sizeof(dist));
dist[1] = 0;
pq.push({0,1,0});
while(!pq.empty()){
ll du,u,c;
tie(du,u,c) = pq.top();
// cerr<<du<<' '<<u<<' '<<c<<endl;
pq.pop();
if (c){
if (dp[u][c] < -du) continue;
ll v,color,w,cost;
for(iii e: g[u][c]){
v = e.fi;
color = e.se.fi;
w = e.se.se;
cost = -du + psum[u][color] - w;
//we flip i.
if (dist[v] > cost){
dist[v] = cost;
pq.push({-dist[v],v,0});
}
}
}
else{
if (dist[u] < -du) continue;
ll v,color,w,cost;
for(auto &i: g[u]){
for(iii e: i.se){
v = e.fi;
color = e.se.fi;
w = e.se.se;
// cout<<v<<' '<<color<<' '<<w<<endl;
cost = -du + psum[u][color] - w;
if (dist[v] > cost){
dist[v] = cost;
pq.push({-dist[v],v,0});
}
cost = -du + w;
if (dist[v] > cost){
dist[v] = cost;
pq.push({-dist[v],v,0});
}
cost = -du;
if (!dp[v].count(color) or dp[v][color] > cost){
dp[v][color] = cost;
pq.push({-dp[v][color],v,color});
}
}
}
}
}
cout<<((dist[n] >= INF) ? -1 : dist[n])<<endl;
exit(0);
}
Compilation message (stderr)
Main.cpp:34:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
34 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:43:16: warning: unused variable 'w' [-Wunused-variable]
43 | int u,v,w,c,p;
| ^
Main.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |