#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define sz(v) (int)v.size()
typedef pair<int,int> pi;
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a; i<b; i++)
void IO(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
//------------------------------------------------------//
bool ckmin(ll &x, ll y){
if(x<=y) return 0;
x=y; return 1;
}
struct state{
int v,c,w;
};
const int MX=1e3+10;
const ll INF=1e18;
int N,M;
vector<state> adj[MX];
int main(){
IO();
cin>>N>>M;
FOR(i,0,M){
int u,v,c,w; cin>>u>>v>>c>>w; u--; v--;
adj[u].pb(state{v,c,w});
adj[v].pb(state{u,c,w});
}
//precompting can
map<int,int>can[N];
FOR(u,0,N){
unordered_map<int,int>mp;
for(auto nxt: adj[u]){
mp[nxt.c]++;
}
int nb=0,color=-1;
FOR(i,1,M+1) if(!mp.count(i)) color=i,nb++;
for(auto nxt: adj[u]){
int v=nxt.v,c=nxt.c,w=nxt.w;
int cur_color=color;
if(mp[c]==1){
nb++;
cur_color=c;
}
if(nb>1) can[u][v]=-1;
else can[u][v]=cur_color;
if(mp[c]==1) nb--;
}
}
//end
ll dist[N][N]; FOR(i,0,N) FOR(j,0,N) dist[i][j]=INF;
dist[0][0]=0;
priority_queue<pair<ll,pi>,vector<pair<ll,pi>>,greater<pair<ll,pi>>> q;
q.push({0,{0,0}});
while(sz(q)){
pair<ll,pi>cur=q.top();
q.pop();
int u=cur.se.fi,p=cur.se.se; ll d=cur.fi;
if(d>dist[u][p]) continue;
unordered_map<int,int>mp;
for(auto nxt: adj[u]) if(nxt.v!=p){
mp[nxt.c]++;
}
for(auto nxt: adj[u]) if(nxt.v!=p){
int v=nxt.v,c=nxt.c,w=nxt.w;
if(mp[c]>1 || (u!=p && can[p][u]==c)){
if(ckmin(dist[v][u],d+w)) q.push({d+w,{v,u}});
}
else{
if(ckmin(dist[v][u],d)) q.push({d,{v,u}});
}
}
}
ll ans=INF;
FOR(i,0,N) ckmin(ans,dist[N-1][i]);
if(ans==INF) ans=-1;
cout << ans << endl;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:61:24: warning: unused variable 'w' [-Wunused-variable]
61 | int v=nxt.v,c=nxt.c,w=nxt.w;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Incorrect |
156 ms |
8424 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
460 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Incorrect |
156 ms |
8424 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |