uva 10048(最短路)

题目的意思就是有n个城市,有m条街,每条街有噪音

问你从起点到终点,最少要忍受多大的噪音(注意不是和,是经过的街中最大的那个.)


AC代码:

#include
const int N = 105 ;
const int INF = 0x3f3f3f3f;
int g[N][N];
int c,s,q;
int main () {
	int a,b,d;
	int cas = 1;
	int first = 1;
	while(scanf("%d%d%d",&c,&s,&q) && c) {
		if (first == 1) {
			first++;
		}
		else {
			printf("\n");
		}
		for (int i = 1 ; i <= c ;i++) {
			for (int j = 1 ;  j <= c ;j++) {
				g[i][j] = INF;
			}
		}
		for (int i = 0 ; i < s ;i++) {
			scanf("%d%d%d",&a,&b,&d);
			g[a][b] = d;
			g[b][a] = d;
		}
		for (int k = 1 ; k <= c ;k++) {
			for (int i = 1 ; i <= c ;i++) {
				for (int j = 1 ; j <= c ;j++) {
					if(g[i][k] != INF && g[k][j] != INF) {
						int m = g[i][k] > g[k][j] ? g[i][k] : g[k][j];
						if(m < g[i][j])
							g[i][j] = m;
					}
				}
			}
		}
		printf("Case #%d\n",cas++);
		for (int i = 0 ; i < q ;i++) {
			scanf("%d%d",&a,&b) ;
			if(g[a][b] < INF)
				printf("%d\n",g[a][b]);
			else
				printf("no path\n");
		}
	}
}


你可能感兴趣的:(DP,图论)