이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
我是不是要給自己設定一個想題目的時間
hard - 50 分鐘
medium - 30 分鐘
easy - 15 分鐘
*/
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1e5+5;
vector<pair<int,pair<int,int>>> adj[mxn];
unordered_map<int,vector<pair<int,int>>> adjc[mxn];
unordered_map<int,ll> csum[mxn];
ll ans[mxn];
unordered_map<int,ll> ans2[mxn];
int main() {_
//setIO("wayne");
// 1. change edge
// 2. change other edges
// 3. change edge and leave next node by edge with same color
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c,w;
cin>>a>>b>>c>>w;
adj[a].push_back({b,{c,w}});
adj[b].push_back({a,{c,w}});
adjc[a][c].push_back({b,w});
adjc[b][c].push_back({a,w});
csum[a][c]+=w;
csum[b][c]+=w;
}
//{-cost,node,color(if case3)}
priority_queue<tuple<ll,int,int>> pq;
pq.push({0,1,0});
for(int i=1;i<=n;i++) ans[i]=(ll)(1e18);
ans[1]=0;
while(!pq.empty()){
ll val;
int v,c;
tie(val,v,c)=pq.top();
val*=-1;
pq.pop();
if(c){
if(ans2[v][c]!=val) continue;
for(auto u:adjc[v][c]){
//if(u.s.f!=c) continue;
ll res=val+csum[v][c]-u.s;
if(res<ans[u.f]){
ans[u.f]=res;
pq.push({-1*ans[u.f],u.f,0});
}
}
}
else{
if(ans[v]!=val) continue;
for(auto u:adj[v]){
ll val1=val+u.s.s;
if(val1<ans[u.f]){
ans[u.f]=val1;
pq.push({-1*ans[u.f],u.f,0});
}
ll val2=val+csum[v][u.s.f]-u.s.s;
if(val2<ans[u.f]){
ans[u.f]=val2;
pq.push({-1*ans[u.f],u.f,0});
}
ll val3=val;
if(ans2[u.f].count(u.s.f)==0 or val3<ans2[u.f][u.s.f]){
ans2[u.f][u.s.f]=val3;
pq.push({-1*ans2[u.f][u.s.f],u.f,u.s.f});
}
}
}
}
if(ans[n]==(ll)1e18){
cout<<-1;
}
else{
cout<<ans[n];
}
return 0;
}
//maybe its multiset not set
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((s + ".out").c_str(), "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... |