이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> llll;
const ll INF=1e18;
const int N=1e5+5;
struct edge
{
int v,c;
ll p;
};
/**
- Can always change an edge to another random color
- Call the random color 0 (can ignore it -> put into dp[u])
Init:
- dp[u]: minimum to get to u
- dp1[u][c]: minimum to get to u, last color is c, didn't count cost to change color of that edge
- ps[u][c]: sum of edge with 1 node is u and color c
If move from color c (c != 0): must move to u with color c -> change all same color edge
If move from color 0:
- Case 1: change all same color edge
- Case 2: change this edge, not all same color edge
- Case 3: move -> dp1[u][c]
**/
map<int,vector<edge>> graph[N];
ll dp[N];
map<int,ll> dp1[N],ps[N];
void solve()
{
int n,m;
cin >> n >> m;
for(int i=1;i<=m;i++)
{
int u,v,c;
ll p;
cin >> u >> v >> c >> p;
graph[u][c].push_back({v,c,p});
graph[v][c].push_back({u,c,p});
ps[u][c]+=p;
ps[v][c]+=p;
}
for(int i=1;i<=n;i++) dp[i]=INF;
dp[1]=0;
priority_queue<pair<ll,pii>,vector<pair<ll,pii>>,greater<pair<ll,pii>>> pq;
pq.push({0,{1,0}});
while((int)pq.size())
{
ll temp=pq.top().fi;
int u=pq.top().se.fi,c=pq.top().se.se;
pq.pop();
if(c)
{
if(dp1[u][c]!=temp) continue;
for(int i=0;i<(int)graph[u][c].size();i++)
{
edge e=graph[u][c][i];
ll w1=ps[u][c]-e.p;
if(dp1[u][c]+w1<dp[e.v])
{
dp[e.v]=w1+dp1[u][c];
pq.push({dp[e.v],{e.v,0}});
}
}
}
else
{
if(dp[u]!=temp) continue;
for(map<int,vector<edge>>::iterator it=graph[u].begin();it!=graph[u].end();it++)
{
for(int i=0;i<(int)it->se.size();i++)
{
edge e=it->se[i];
ll w1=ps[u][e.c]-e.p,w2=e.p;
if(dp[u]+min(w1,w2)<dp[e.v])
{
dp[e.v]=dp[u]+min(w1,w2);
pq.push({dp[e.v],{e.v,0}});
}
if(!dp1[e.v].count(e.c)||dp[u]<dp1[e.v][e.c])
{
dp1[e.v][e.c]=dp[u];
pq.push({dp1[e.v][e.c],{e.v,e.c}});
}
}
}
}
}
if(dp[n]>=INF) dp[n]=-1;
cout << dp[n] << endl;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("hanhhhh.inp","r",stdin);
//freopen("hanhhhh.out","w",stdout);
int t=1;
//cin >> t;
while(t--)
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |