POJ-1797 Heavy Transportation
题意:
思路:(虽然被放到最短路专题上但是我却想到生成树的问题)就是给出n个位置,有m条边 , 给出m条边的信息,然后求点1到点n处 中的一条路,使得运载最大(这个运载量是取的该路径中沉重最小的量)
就是像prim构造最小生成树一样构造最大生成树
完整代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> const int maxn = 1e3+9; using namespace std; int n, m; int g[maxn][maxn]; int cnt; void search() { int vis[maxn]; int dist[maxn]; //用于保存街道的最大承重量 int i, j, cur, Max; for (i = 1; i <= n; i++) { vis[i] = 0; dist[i] = g[1][i]; } vis[1] = 1; dist[1] = 0; for (i = 2; i <= n; i++) { Max = -1; for (j = 1; j <= n; j++) { if (!vis[j] && (dist[j] > Max)) //找出路径中最大承重 { Max = dist[j]; cur = j; } } vis[cur] = 1; for (j = 1; j <= n; j++) { if (!vis[j]) { int Min = min(dist[cur],g[cur][j]); //判断当前点和与之相邻街道的 求出最小的承重 if (dist[j] < Min) //当小于最小的承重,则进行转换 { dist[j] = Min; } } } } cnt ++; printf("Scenario #%d:\n", cnt); printf("%d\n\n", dist[n]); } int main() { int t; scanf("%d", &t); cnt = 0; while (t--) { int i, j; int a, b, c; scanf("%d %d", &n, &m); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { g[i][j] = 0; //初始化街道的承重都为零 } } for (i = 1; i <= m; i++) { scanf("%d %d %d", &a, &b, &c); g[a][b] = g[b][a] = c; //一步一步往地图中添加街道的承重 } search(); } return 0; }