#include <iostream>
#include <climits>
#include <queue>
#include <fstream>
using namespace std;
const int N=1e5+5;
const long long INF=1e18;
struct E{
int v, c, w;
void tie(int &_v, int &_c, int &_w)
{
_v=v;
_c=c;
_w=w;
}
};
vector<E>g[N];
int n, m, u, v, c, w;
long long d[N], ans[2*N], sum[2*N], tmp;
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>>q;
void bfs()
{
for(int i=1;i<=n;i++)
d[i]=INF;
for(int i=1;i<=m;i++)
ans[i]=INF;
d[1]=0;
q.push({0,1});
while(!q.empty())
{
auto [du,u]=q.top();
q.pop();
if(du!=d[u])
continue;
for(auto &e:g[u])
{
e.tie(v,c,w);
sum[c]+=w;
ans[c]=min(ans[c],d[v]);
}
for(auto &e:g[u])
{
e.tie(v,c,w);
tmp=min(1LL*w,sum[c]-w);
if(d[v]>du+tmp)
{
d[v]=du+tmp;
q.push({d[v],v});
}
if(d[v]>ans[c]+sum[c]-w)
{
d[v]=ans[c]+sum[c]-w;
q.push({d[v],v});
}
}
for(auto &e:g[u])
{
sum[e.c]=0;
ans[e.c]=INF;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if(fopen("robot1.inp", "r")) {
freopen("robot1.inp", "r", stdin);
freopen("robot1.out", "w", stdout); }
cin>>n>>m;
while(m--)
{
cin>>u>>v>>c>>w;
g[u].push_back({v,c,w});
g[v].push_back({u,c,w});
}
bfs();
cout<<(d[n]==INF?-1:d[n]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen("robot1.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen("robot1.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... |