记录编号 |
246891 |
评测结果 |
AAAAAAAAAA |
题目名称 |
龙凡 |
最终得分 |
100 |
用户昵称 |
TenderRun |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
2.464 s |
提交时间 |
2016-04-07 16:10:08 |
内存使用 |
15.73 MiB |
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=100010;
const int maxm=400010;
int cnt,fir[maxn],to[maxm],nxt[maxm],val[maxm];
void addedge(int a,int b,int c){
nxt[++cnt]=fir[a];to[cnt]=b;fir[a]=cnt;val[cnt]=c;
}
int tot,rt[maxm],key[maxm],lik[maxm],ch[maxm][2],dep[maxm];
int ans[maxn],dis[maxn],add[maxm];
void Add(int x,int d){
if(!x)return;
key[x]+=d;
add[x]+=d;
}
void Push_down(int x){
Add(ch[x][0],add[x]);
Add(ch[x][1],add[x]);
add[x]=0;
}
int Merge(int x,int y){
if(!x||!y)return x|y;
if(key[x]>key[y])swap(x,y);
Push_down(x);
ch[x][1]=Merge(ch[x][1],y);
if(dep[ch[x][0]]<dep[ch[x][1]])
swap(ch[x][0],ch[x][1]);
dep[x]=dep[ch[x][1]]+1;
return x;
}
void Delete(int node){
Push_down(rt[node]);
rt[node]=Merge(ch[rt[node]][0],ch[rt[node]][1]);
}
int ID[maxn],end[maxn],id;
void DFS(int node){
ID[node]=++id;
for(int i=fir[node];i;i=nxt[i])
if(dis[to[i]]==dis[node]+val[i])
DFS(to[i]);
end[node]=id;
}
void Solve(int node){
for(int i=fir[node];i;i=nxt[i]){
if(dis[to[i]]==dis[node]+val[i]){
Solve(to[i]);
Add(rt[to[i]],val[i]);
rt[node]=Merge(rt[node],rt[to[i]]);
}
else{
if(dis[to[i]]+val[i]==dis[node])continue;
if(ID[to[i]]<=end[node]&&ID[to[i]]>=ID[node])continue;
key[++tot]=dis[to[i]]+val[i];lik[tot]=to[i];
rt[node]=Merge(rt[node],tot);
}
}
while(rt[node]&&ID[lik[rt[node]]]<=end[node]&&ID[lik[rt[node]]]>=ID[node]){
Delete(node);
}
if(rt[node])
ans[node]=key[rt[node]];
}
struct Node{
int d,n;
Node(int d_=0,int n_=0){
d=d_;n=n_;
}
bool operator <(const Node &b)const{
return d>b.d;
}
};
priority_queue <Node,vector<Node> >q;
int main(){
freopen("travel.in","r",stdin);
freopen("travel.out","w",stdout);
int n,m;
dep[0]=-1;
scanf("%d%d",&n,&m);
for(int i=1,a,b,c;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
memset(dis,63,sizeof(dis));
q.push(Node(0,1));dis[1]=0;
while(!q.empty()){
Node x=q.top();q.pop();
for(int i=fir[x.n];i;i=nxt[i]){
if(dis[x.n]+val[i]<dis[to[i]]){
dis[to[i]]=dis[x.n]+val[i];
q.push(Node(dis[to[i]],to[i]));
}
}
}
DFS(1);
Solve(1);
for(int i=2;i<=n;i++)
printf("%d\n",ans[i]==0?-1:ans[i]);
return 0;
}