POJ - 1797 Heavy Transportation

来源:互联网 发布:怎么分析数据 编辑:程序博客网 时间:2024/06/09 23:59

原题链接

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input
1
3 3
1 2 3
1 3 4
2 3 5

Sample Output
Scenario #1:
4

思路1:dijkstra算法
d[i]不再是从起点到i点的最短距离,而是从起点到i点过程中的最小承重限制。
不断从未访问的点中挑选出d[v]最大的点v,再更新其他点的d[i]值,即d[i]=max(d[i],min(d[v],weight[v][i]))

AC代码:

#include <iostream>#include <cstdio>  #include <cstring>  #include <cstdlib>#include <cmath>#include <algorithm>  using namespace std; int t, n, m, weight[1005][1005], vis[1005], d[1005];int dijkstra(){      int i, j;      fill(vis + 1, vis + 1 + n, 0);    for(i = 1; i <= n; i++){        d[i] = weight[1][i];    }      for(i = 1; i <= n; i++){          int max_weight = -1;        int v;        for(j = 1; j <= n; j++){            if(!vis[j] && d[j] > max_weight){                  max_weight = d[j];                   v = j;               }        }        vis[v] = 1;           for(j = 1; j <= n; j++){              if(!vis[j] && d[j] < min(d[v], weight[v][j])){                  d[j] = min(d[v], weight[v][j]);               }          }    }      return d[n];   }  int main(){      int i, j, k;       scanf("%d", &t);       for(k = 1; k <= t; k++){          scanf("%d %d",  &n,  &m);           for(i = 1; i <= n; i++)   fill(weight[i] + 1, weight[i] + 1 + n, 0);        for(i = 1; i <= m; i++){              int a, b, c;            scanf("%d %d %d", &a, &b, &c);               weight[a][b] = weight[b][a]=c;           }          printf("Scenario #%d:\n%d\n\n", k, dijkstra());         }      return 0;   }  

思路2:kruskal算法
从权值最大的边开始,依次判断它是否能加入并查集,直到起点和终点连通为止,此时加入的最后一条边的权值是所求答案。

AC代码:

#include<cstdio>#include<algorithm>using namespace std;struct Edge{    int from, to, cost;}es[1000005]; int N,M;int par[1005];int rank[1005];//并查集 void init(int n){    for(int i = 1; i <= n; ++i){        par[i] = i;        rank[i] = 1;    }}int find(int x){    if(x == par[x])  return x;    else  return par[x] = find(par[x]);}void unite(int x, int y){     x = find(x);     y = find(y);    if(x == y)  return;    if(rank[x] < rank[y])  par[x] = y;    else{        par[y] = x;        if(rank[x] == rank[y])  rank[x]++;    }}bool same(int x, int y){     return find(x) == find(y); }bool EdgeCmp(Edge &e1, Edge &e2){     return e1.cost > e2.cost;     }int Kruskal(){    sort(es, es + M, EdgeCmp);    init(N);    for(int i = 0; i < M; ++i){        Edge &e = es[i];        if(!same(e.from, e.to)){            unite(e.from, e.to);            if(same(1, N))  return e.cost;        }    }    return -1;}int main(){    int T;    scanf("%d",&T);    for(int t = 1; t <= T; ++t){        scanf("%d %d", &N, &M);        for(int m = 0; m < M; ++m)            scanf("%d %d %d", &es[m].from, &es[m].to, &es[m].cost);        printf("Scenario #%d:\n%d\n\n", t, Kruskal());  //注意题目要求留空白行,所以是\n\n     }    return 0;}
1 0
原创粉丝点击