# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
939448 | koukirocks | Robot (JOI21_ho_t4) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=1e5+10,P=998244353;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
ll n,m;
struct edge{
ll des,clr,cst;
edge(ll _des,ll _clr,ll _cst):des(_des),clr(_clr),cst(_cst){}
};
vector<edge> G[MAX];
vector<pll> G2[MAX*3];
int main() {
speed;
cin>>n>>m;
for (int i=0;i<m;i++) {
ll a,b,c,p;
cin>>a>>b>>c>>p;
G[a].emplace_back(b,c,p);
G[b].emplace_back(a,c,p);
G2[a].emplace_back(b,p);
G2[b].emplace_back(a,p);
}
ll now=n+1;
for (int i=1;i<=n;i++) {
map<int,vector<pll> > ttl;
for (auto eg:G[i]) ttl[eg.clr].emplace_back(eg.des,eg.cst);
for (auto [clr,egs]:ttl) {
ll ttlw=0;
for (auto eg:egs) ttlw+=eg.second;
G2[i].push_back({now,0});
for (auto eg:egs) {
G2[now].emplace_back(eg.first,ttlw-eg.second);
G2[eg.first].emplace_back(now,0);
}
// cout<<i<<" "<<clr<<" "<<now<<"\n";
now++;
}
}
// for (int i=1;i<now;i++) {
// cout<<i<<".\n";
// for (auto j:G2[i]) {
// cout<<j.first<<" "<<j.second<<"\n";
// }
// cout<<"----\n";
// }
priority_queue<pll> D;
vector<bool> vis(now+1,0);
vector<ll> dis(now+1,oo);
dis[1]=0;
D.emplace(0,1);
while (!D.empty()) {
while (!D.empty() and vis[D.top().second]) D.pop();
if (D.empty()) break;
int v=D.top().second;
D.pop();
vis[v]=true;
for (auto [u,w]:G2[v]) {
if (!vis[u] and dis[v]+w<dis[u]) {
dis[u]=dis[v]+w;
D.emplace(-dis[u],u);
}
}
}
cout<<(dis[n]==oo?"-1":dis[n])<<"\n";
return 0;
}