国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ - 1797 Heavy Transportation

2019-11-08 02:54:19
字體:
來源:轉載
供稿:網友

原題鏈接

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼勒克县| 西林县| SHOW| 江达县| 凌海市| 康马县| 中江县| 深泽县| 浦东新区| 扶风县| 嘉义县| 依安县| 郸城县| 武山县| 闻喜县| 青神县| 彭州市| 当阳市| 沂源县| 广平县| 车险| 金坛市| 安丘市| 刚察县| 涡阳县| 无为县| 周口市| 大同县| 潢川县| 新邵县| 桐柏县| 连城县| 通榆县| 南宫市| 观塘区| 文化| 新田县| 绥中县| 龙陵县| 文登市| 龙南县|