//2577833 2010-07-08 14:29:13 Accepted 1874 15MS 284K 1445 B C++ T&T
#include<iostream>
#include<cstdio>
#include<queue>
#define MAX 0x7fffffff
#define asize 210
using namespace std;
struct node{
node(int vv=0,int dd=0):vexn(vv),dist(dd){}
int vexn;
int dist;
friend bool operator<(node a,node b)
{
return a.dist > b.dist;
}
};
bool hash[asize];
int dis[asize],cost[asize][asize];
int count1;
int djkstra(int v0,int n,int t)
{
priority_queue<node> q;
int i;
for(i = 0; i < n; i++)
{
dis[i] = MAX;
//path[i] = -1;
}
memset(hash,true, sizeof(hash));
dis[v0] = 0;
//path[v0] = 0;
q.push(node(v0,dis[v0]));
node N;
while(!q.empty())
{
N = q.top();
q.pop();
if(!hash[N.vexn])continue;
hash[N.vexn] = false;
for(i = 0; i < n; i++)
{
if(hash[i] && N.dist + cost[N.vexn][i] < dis[i]&& cost[N.vexn][i] < MAX)
{
dis[i] = N.dist + cost[N.vexn][i];
//path[i] = N.vexn;
q.push(node(i,dis[i]));
}
}
}
if(dis[t] == MAX)
return -1;
return dis[t];
}
int main()
{
int n,i,w,m,b,j,a;
int s,t;
while(scanf("%d%d",&n,&m) != EOF)
{
count1 = 0;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
{
if(i == j)
cost[i][j] = 0;
else
cost[i][j] = MAX;
}
for(i = 0; i < m; i++)
{
scanf("%d%d%d",&a,&b,&w);
if(w < cost[a][b])
{
cost[a][b] = w;
cost[b][a] = w;
}
}
scanf("%d%d",&s,&t);
int res = djkstra(s,n,t);
printf("%d/n",res);
}
}
hdu 1874
最新推荐文章于 2022-02-25 21:57:21 发布