RT,DJ是不是不能跑最长路啊...
小蒟蒻打了个DJ想跑最长路,然后挂了.
Code:
#include<bits/stdc++.h>
using namespace std;
#define re register
#define N 200510
int t,sx,sy,n,m,cnt=1,h[N],kx,ky,d,dis[N],vis[N];
struct STAR{int v,w,nxt;}e[N<<4];
void add(int u,int v,int w){e[++cnt]=(STAR){v,w,h[u]};h[u]=cnt;}
struct pos
{
int x,num;
bool operator < (const pos &a)const
{
return num<a.num;
}
};
priority_queue<pos>q;
void read(int &x)
{
x=0;char ch=getchar();
while(ch>57||ch<48)ch=getchar();
while(ch>47&&ch<58)x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
}
int main()
{
memset(h,0,sizeof(h));cnt=0;
memset(vis,0,sizeof(vis));
cin>>n>>m;
for(re int i=1;i<=m;++i)
read(kx),read(ky),read(d),add(kx,ky,d);
q.push((pos){1,0});
while(!q.empty())
{
re int u=q.top().x;q.pop();
if(vis[u])continue;vis[u]=1;
for(re int i=h[u];i;i=e[i].nxt)
{
re int v=e[i].v;
if(dis[v]<dis[u]+e[i].w)
{
dis[v]=dis[u]+e[i].w;
q.push((pos){v,dis[v]});
}
}
}
for(re int i=1;i<=n;++i)
printf("%d ",dis[i]);
puts("");
return 0;
}
Hack掉我的数据:
5 5
1 2 3
2 3 10
1 4 1
4 3 17
3 5 1
Answer:
0 3 18 1 19
My answer:
0 3 18 1 14