# 畅通工程续

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 43164    Accepted Submission(s): 16007

Problem Description

Input

Output

Sample Input
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2

Sample Output
2 -1

Author
linle

Source

Recommend
lcy   |   We have carefully selected several similar problems for you:  2066 2112 1217 1233 1142

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define N 1100

int n,m;
int dis[N],maps[N][N],vis[N];

void init(int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
maps[i][j]=(i==j)?0:INF;
}

int dijkstra(int s,int e)
{
int i,j,Min,index;
for(i=0;i<n;i++)
{
dis[i]=maps[s][i];
vis[i]=0;
}
vis[s]=1;
for(i=0;i<n-1;i++)
{
Min=INF;
for(j=0;j<n;j++)
{
if(!vis[j] && dis[j]<Min)
{
Min=dis[j];
index=j;
}
}
vis[index]=1;
for(j=0;j<n;j++)
{
if(!vis[j] && dis[j]>Min+maps[index][j])
{
dis[j]=Min+maps[index][j];
}
}
}
return dis[e];
}

int main()
{
int i,a,b,c,s,e,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
init(n);
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
maps[a][b]=maps[b][a]=min(maps[a][b],c);
}
scanf("%d%d",&s,&e);
ans=dijkstra(s,e);
if(ans==INF)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}


#### HDU1232——畅通工程（并查集）

2015-12-04 18:32:12

#### hdu1863 畅通工程

2016-05-28 14:09:06

#### HDU1863(畅通工程)

2017-08-03 20:43:23

#### HDU 1875 畅通工程再续

2014-04-13 11:47:13

#### hdu1863-畅通工程

2015-08-26 20:04:20

#### HDU杭电1874-畅通工程续（dijkstra算法和Floyd算法）

2017-02-21 19:34:42

#### HDU1874：畅通工程续(最短路Dijkstra(n^2+nlogn)+Floyd+SPFA(堆栈+队列))

2013-11-21 23:19:51

#### ACM-并查集之畅通工程——hdu1232

2014-04-16 09:21:28

#### hdu1874(Dijkstra&&spfa)

2015-09-17 20:45:35

#### ACM-最小生成树之畅通工程——hdu1863

2014-05-25 11:04:41