結果
| 問題 |
No.1602 With Animals into Institute 2
|
| コンテスト | |
| ユーザー |
bin101
|
| 提出日時 | 2022-04-13 01:53:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,000 ms / 4,000 ms |
| コード長 | 4,294 bytes |
| コンパイル時間 | 2,697 ms |
| コンパイル使用メモリ | 195,628 KB |
| 実行使用メモリ | 35,564 KB |
| 最終ジャッジ日時 | 2024-12-21 14:49:49 |
| 合計ジャッジ時間 | 16,996 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
//パラレル3本以上あり
//
#include<bits/stdc++.h>
using namespace std;
using ll=long long int;
//T: 距離 S: ラベル(群) U: 群の演算
template<typename T,typename S,typename U>
struct ShortestNonzeroPath{
struct edge{
int from;
int to;
T cost;
S label;
int idx;
//edge(){}
bool operator<(edge e)const{
return from<e.from;
}
};
int V; //頂点数
T inf;
vector<T> q; //shortest unorthodox path distance
vector<int> parent;
S identity;
int s; //始点
vector<T> dist;
vector<int> depth;
vector<S> psi;
vector<vector<edge>> G;
vector<int> pred;
priority_queue<pair<T,edge>,vector<pair<T,edge>>,greater<>> F;
vector<T> h;
vector<T> ans;
int E; //辺数
U f;
void add_edge(int from,int to,T cost,S label){
G[from].push_back({from,to,cost,label,E});
E++;
}
bool is_consistent(edge e){
if(f(psi[e.from],e.label)==psi[e.to]) return true;
return false;
}
ShortestNonzeroPath(int V,int identity,int s,U f):V(V),inf(numeric_limits< T >::max()),q(V,inf),
identity(identity),s(s),dist(V,inf),depth(V,inf),psi(V,identity),
G(V),pred(V),ans(V),E(0),f(f){
}
void build(){
h.resize(E,inf);
Dijkstra();
Step1();
Step2();
while(F.size()) Step3();
ans.resize(V);
for(int i=0;i<V;i++){
if(psi[i]==identity) ans[i]=q[i];
else ans[i]=dist[i];
}
}
//始点: 0
void Dijkstra(){
using pti=pair<T,int>;
priority_queue<pti,vector<pti>,greater<pti>> que;
dist[s]=0;
psi[s]=identity;
depth[s]=0;
que.emplace(dist[s],s);
while(!que.empty()){
T cost;
int idx;
tie(cost,idx)=que.top();
que.pop();
if(dist[idx]<cost) continue;
for(auto &e:G[idx]){
auto next_cost=cost+e.cost;
if(dist[e.to]<=next_cost) continue;
pred[e.to]=idx;
depth[e.to]=depth[idx]+1;
psi[e.to]=f(psi[idx],e.label);
dist[e.to]=next_cost;
que.emplace(dist[e.to],e.to);
}
}
}
void Step1(){
parent.resize(V);
for(int v=0;v<V;v++) parent[v]=v;
}
void Step2(){
for(int v=0;v<V;v++){
for(auto e:G[v]){
if(is_consistent(e)) h[e.idx]=inf;
else{
h[e.idx]=dist[v]+dist[e.to]+e.cost;
F.emplace(h[e.idx],e);
}
}
}
}
int root(int x){
if(x==parent[x]) return x;
else return parent[x]=root(parent[x]);
}
void Step3(){
//3_1 start
T delta;
edge e;
tie(delta,e)=F.top();
F.pop();
if(delta!=h[e.idx]) return;
array<int,2> w;
w[0]=root(e.from);
w[1]=root(e.to);
vector<int> B;
//3_1 end
while(w[0]!=w[1]){
if(depth[w[0]]<depth[w[1]]) swap(w[0],w[1]); //0の方を深くする 0を親に移動する
B.push_back(w[0]);
w[0]=root(pred[w[0]]);
}
//3_2 end
int w_1=w[0];
for(int w:B){
parent[w]=w_1;
q[w]=h[e.idx]-dist[w];
for(auto e:G[w]){
if(not is_consistent(e)) continue;
T cost=q[w]+dist[e.to]+e.cost;
if(h[e.idx]>cost){
h[e.idx]=cost;
F.emplace(cost,e);
}
}
}
}
};
signed main(){
int N,M,K;
cin>>N>>M>>K;
auto f=[](int a,int b){return a^b;};
ShortestNonzeroPath<ll,int,decltype(f)> S(N,0,N-1,f);
while(M--){
int A,B,C;
string X;
cin>>A>>B>>C>>X;
A--; B--;
int x=0,x_=0;
for(int i=0;i<K;i++){
if(X[i]=='1') x+=(1<<i);
else x_+=(1<<i);
}
S.add_edge(A,B,C,x);
S.add_edge(B,A,C,x);
}
S.build();
for(auto &i:S.ans) if(i==S.inf) i=-1;
for(int i=0;i<N-1;i++){
cout<<S.ans[i]<<endl;
}
//cerr<<S.parent[2]<<endl;
}
bin101