#include <bits/stdc++.h>
using namespace std;
const int N=1e5+1;
const int M=2e5+1;
int n,m;
map<long long,long long>mp[N];
vector<pair<int,pair<int,int>>>adj[N];
long long dp[N];
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
dp[i]=1e18;
for(int i=1; i<=m; i++)
{
int a,b,color,c;
cin>>a>>b>>color>>c;
adj[a].push_back({b,{color,c}});
adj[b].push_back({a,{color,c}});
mp[a][color]+=c;
mp[b][color]+=c;
}
dp[1]=0;
priority_queue<pair<int,int>> pq;
pq.push({0,1});
while(!pq.empty())
{
int cost=-pq.top().first;
int nod=pq.top().second;
pq.pop();
for(auto u:adj[nod])
{
int cost1=u.second.second;
int cost2=mp[nod][u.second.first]-cost1;
cost1=min(cost1,cost2);
if(dp[u.first]>cost1+dp[nod])
{
dp[u.first]=cost1+dp[nod];
pq.push({-dp[u.first],u.first});
}
}
}
if(dp[n]==1e18)
{
cout<<-1<<'\n';
return 0;
}
cout<<dp[n]<<'\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:29:13: warning: unused variable 'cost' [-Wunused-variable]
29 | int cost=-pq.top().first;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
1 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Execution timed out |
3122 ms |
533376 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
15440 KB |
Output is correct |
2 |
Correct |
32 ms |
11868 KB |
Output is correct |
3 |
Correct |
95 ms |
16468 KB |
Output is correct |
4 |
Correct |
45 ms |
13140 KB |
Output is correct |
5 |
Incorrect |
308 ms |
30180 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
1 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Execution timed out |
3122 ms |
533376 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |