POJ-1797(dijstra变形)(Heavy Transportation)

/*==========================================================*\
| Hugo Heavy要从城市1到城市N运送货物,有M条道路,每条道路都有
| 它的最大载重量,问从城市1到城市N运送最多的重量是多少?
\*==========================================================*/
#define N 1005
bool visit[N];
int dis[N];
#define MAX_DISTANCE 0x7fffffff
int path[N];
int g[N][N];
int dijkstra(int n, int s, int t)
{
	int i, j, p;
	memset(visit, 0, sizeof(visit));;
	for (i = 1; i <= n; ++i) {
		dis[i] = 0;
		path[i] = -1;
	}
	dis[s] = 0;
	for (i = 1; i <= n; ++i) {
		p = -1;
		int max = 0;
		for (j = 1; j <= n; ++j) {//找到没访问过而且距离源点最近的点
			if (visit[j] == 0 && (p == -1 || dis[j] > dis[p])) {//【变形】找离源点最远的点
				p = j;
			}
		}
		visit[p] = 1;//访问之
		if (p == t) break;
		for (j = 1; j <= n; ++j) {
			if (visit[j] == 0 && g[p][j] != 0 && g[p][j] > dis[j]) {//【变形】只存边的权值
				dis[j] = g[p][j];
				path[j] = p;
			}
		}
	}	
	int ans = MAX_DISTANCE;
	while (path[p] != -1) {		
		ans = MY_MIN(ans, g[path[p]][p]);
		p = path[p];
	}
	return ans;
}
int main()
{
	int cases, m, n;
	scanf("%d", &cases);
	int index = 1;
	while (cases--) {
		memset(g, 0, sizeof(g));
		int m, n;
		int x, y, z, i;
		scanf("%d%d", &n, &m);
		for (i = 0; i < m; ++i) {
			scanf("%d%d%d", &x, &y, &z);
			g[x][y] = g[y][x] = z;
		}
		printf("Scenario #%d:\n%d\n\n", index++, dijkstra(n, 1, n));
	}
	return 0;
}

你可能感兴趣的:(Path,distance)