//最短路问题,这里要理解flody算法的过程
//如果已经marked的点,可以当做中间结点,求出经过这个中间结点所有的最短路
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 331
#define INF 0x3f3f3f3f
int each_dis[MAXN][MAXN], marked[MAXN];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int vertex, arc, ask, u, v, w, cas(1);
while( scanf("%d %d %d", &vertex, &arc, &ask) ) {
if( !vertex && !arc && !ask ) {
break;
}
if( 1 != cas ) {
printf("\n");
}
printf("Case %d:\n", cas ++);
for(int i = 0; i < vertex; i ++) {
for(int j = 0; j < vertex; j ++) {
each_dis[i][j] = INF;
}
each_dis[i][i] = 0;
}
for(int i = 0; i < arc; i ++) {
scanf("%d %d %d", &u, &v, &w);
each_dis[u][v] = min(each_dis[u][v], w);
}
memset(marked, 0, sizeof(marked));
for(int t = 0; t < ask; t ++) {
scanf("%d", &w);
if( !w ) {
scanf("%d", &u);
if( marked[u] ) {
printf("ERROR! At point %d\n", u); continue;
}
marked[u] = 1;
for(int i = 0; i < vertex; i ++) {
if( INF == each_dis[i][u] ) {
continue;
}
for(int j = 0; j < vertex; j ++) {
if( INF == each_dis[u][j] ) {
continue;
}
each_dis[i][j] = min(each_dis[i][j], each_dis[i][u]+each_dis[u][j]);
}
}
}
else {
scanf("%d %d", &u, &v);
if( !marked[u] || !marked[v] ) {
printf("ERROR! At path %d to %d\n", u, v); continue;
}
if( INF == each_dis[u][v] ) {
printf("No such path\n"); continue;
}
printf("%d\n", each_dis[u][v]);
}
}
}
return 0;
}
hdu_3631_Shortest Path
最新推荐文章于 2024-09-04 21:39:45 发布