简单的folyd,题意为:给定一个有向图,要求从那个点开始,获得的最少时间,输入起始点和所需要的最少时间。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 101
int map[MAXN][MAXN];
void folyd(const int &n)
{
for(int k = 1; k <= n; k ++) {
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
}
}
}
}
int cal(const int &u, const int &n)
{
int rst(0);
for(int v = 1; v <= n; v ++) {
if( v == u ) {
continue;
}
rst = max(rst, map[u][v]);
}
return rst;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, v, cost, m, start, ans, tmp;
while( scanf("%d", &n) && n ) {
memset(map, 0x3f, sizeof(map));
for(int u = 1; u <= n; u ++) { scanf("%d", &m);
for(; m; m --) {
scanf("%d %d", &v, &cost); map[u][v] = cost;
}
}
folyd(n); ans = 0x3f3f3f3f;
for(int i = 1; i <= n; i ++) {
if( ans > (tmp = cal(i, n)) ) {
ans = tmp; start = i;
}
}
printf("%d %d\n", start, ans);
}
return 0;
}
poj_1125_Stockbroker Grapevine
最新推荐文章于 2020-07-01 00:14:53 发布