3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
迪科斯彻的模板题。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 205;
const int MAX = 0x3f3f3f3f;
const int mod = 1000000007;
int m, n, st, en;
int g[205][205], dij[205], vis[205];
void DIJ() {
memset(dij, MAX, sizeof(dij));
memset(vis, 0, sizeof(vis));
dij[st] = 0;
for(int i = 0; i < n; i++){
int tmp = MAX, pos;
for(int j = 0; j < n; j++)
if(vis[j] == 0 && dij[j] < tmp) {
tmp = dij[j];
pos = j;
}
vis[pos] = 1;
for(int j = 0; j < n; j++)
if(vis[j] == 0 &&dij[pos] + g[pos][j] < dij[j]) dij[j] = dij[pos] + g[pos][j];
}
}
int main()
{
while(~scanf("%d%d", &n, &m)) {
memset(g, MAX, sizeof(g));
for(int i = 1; i <= m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c) ;
if(c > g[a][b]) continue;
g[a][b] = g[b][a] = c;
}
scanf("%d%d", &st, &en);
DIJ();
if(vis[en]) printf("%d\n", dij[en]);
else printf("-1\n");
}
return 0;
}
HDU 1874 畅通公程续 (最短路 水),布布扣,bubuko.com
原文:http://blog.csdn.net/u013923947/article/details/38464849