您的位置:首页 > 其它

HdU 2544 最短路

2016-04-30 23:57 176 查看
明天更新spfa。。。

下面是djstl

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<string>
using namespace std;
struct sb
{
int x;
int t;
sb(int a,int b)
{
x=a;
t=b;
}
bool operator<(const sb&wakaka)const
{
return t>wakaka.t;
}
};
vector<sb>bian[10010];
int n;
int ts[10010];
int vis[10010];
void djstl(int who)
{
for(int i=1;i<=n;i++)
{
ts[i]=0x3f3f3f3f;
vis[i]=0;
}
ts[who]=0;
priority_queue<sb>q;
q.push(sb(who,ts[who]));
while(!q.empty())
{
sb x=q.top();
q.pop();
if(vis[x.x])
{
continue;
}
vis[x.x]=1;
for(int i=0;i<bian[x.x].size();i++)
{
sb y=bian[x.x][i];
if(ts[y.x]>x.t+y.t)
{
ts[y.x]=x.t+y.t;
q.push(sb(y.x,ts[y.x]));
}
}
}
}
int  main()
{
int m;
while(cin>>n>>m)
{
if(n==0&&m==0)
break;
int a,b,t;
for(int i=1;i<=m;i++)
{
cin>>a>>b>>t;
bian[b].push_back(sb(a,t));
bian[a].push_back(sb(b,t));
}
djstl(1);
for(int i=1;i<=n;i++)
{
bian[i].clear();
}
cout<<ts
<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu