标签:
题目大意:
给你一个n个节点,m条边的图,然后,给出q个询问,让你找出一条从u到v的路径中所经过的最大权值最小的路径权值。
解题思路:
直接使用floyd的变形来做。
e[i][j] = min(e[i][j],e[i][k]+e[k][j])---->e[i][j] = min(e[i][j],max(e[i][k],e[k][j]));
代码:
# include<cstdio> # include<iostream> using namespace std; # define MAX 123 # define inf 99999999 int e[MAX][MAX]; int n,m,q; void init() { for ( int i = 1;i <=n ;i++ ) { for ( int j = 1;j <= n;j++ ) { if ( i==j ) e[i][j] = 0; else e[i][j] = inf; } } } void floyd() { for ( int k = 1;k <= n;k++ ) { for ( int i = 1;i <= n;i++ ) { for ( int j = 1;j <= n;j++ ) { if ( e[i][k]<inf&&e[k][j]<inf ) e[i][j] = min(e[i][j],max(e[i][k],e[k][j])); } } } } void print() { for ( int i = 1;i <= n;i++ ) { for ( int j = 1;j <= n;j++ ) { cout<<e[i][j]<<" "; } cout<<endl; } } int main(void) { int icase = 0; while ( scanf("%d%d%d",&n,&m,&q)!=EOF ) { if ( n==0 ) break; init(); while ( m-- ) { int a,b,c; scanf("%d%d%d",&a,&b,&c); e[a][b] = min(e[a][b],c); e[b][a] = c; } if (icase) puts(""); printf("Case #%d\n",++icase); floyd(); // print(); while ( q-- ) { int t1,t2; scanf("%d%d",&t1,&t2); int ans = e[t1][t2]; if (ans!=inf) printf("%d\n",ans); else printf("no path\n"); } } return 0; }
11.3.4 例题11-5 UVA 247 Audiophobia(两点间最大权最小_floyd()变形)
标签:
原文地址:http://www.cnblogs.com/lushichuanshuo/p/4858068.html