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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

POJ1797-Heavy Transportation(Dijkstra 變式& 最大生成樹)

2019-11-10 19:25:04
字體:
供稿:網(wǎng)友

Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32337 Accepted: 8567 Description

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

分析 兩種方法,一種是Dijkstra變式,一種是最大生成樹,在最大生成樹路徑中找權(quán)值最小的邊。

Case 1: Dijkstra 變式(這題與Frog 不同,F(xiàn)rog 是求通路中 希望每條邊盡可能小,這題是希望通路中每條邊盡可能大)

#include<iostream> #include<string.h> #include<algorithm> #include<cstdio> using namespace std; #define INF 0x3f3f3f3f const int maxn= 1005; int dist[maxn]; int vis[maxn]; int g[maxn][maxn]; int fin_cnt; void init(int n){ memset(vis,0,sizeof(vis)); dist[1]=0; vis[1]=1; fin_cnt=1; for(int i=1;i<= n;i++){ dist[i]=g[1][i]; } } void dijkstra(int n){ int MAX,MAX_IDX; while( fin_cnt < n){ MAX=-INF; for(int i=2;i<=n;i++){ if(vis[i] ) continue; if(dist[i] > MAX) MAX=dist[i],MAX_IDX=i; } if(MAX == -INF) break; fin_cnt++; vis[MAX_IDX]=1; for(int i= 2;i<=n;i++){ if(vis[i]) continue; int tmp=min(dist[MAX_IDX],g[MAX_IDX][i]); dist[i]=max(dist[i],tmp); } } } int main(){ // freopen("in.txt","r",stdin); int T; scanf("%d",&T); for(int cas=1;cas <=T;cas ++){ int n,m; scanf("%d%d",&n,&m); int t1,t2,t3; for(int i=0;i<=n;i++){ for(int j=0;j<=n;j++) g[i][j]=(i==j? 0:-INF); } for(int i=0;i<m;i++){ scanf("%d%d%d",&t1,&t2,&t3); g[t1][t2]=g[t2][t1]=t3; } init(n); dijkstra(n); printf("Scenario #%d:/n",cas); printf("%d/n/n",dist[n]); }}

Case 2: 最大生成樹 & kruskal , 但是當(dāng)起點(diǎn)與終點(diǎn) 在一個(gè)集合時(shí)要終止,否則會(huì)受到后續(xù)邊的影響。

#include<iostream> #include<string.h> #include<algorithm> #include<cstdio> #include<vector> using namespace std; #define INF 0x3f3f3f3f const int maxn= 1005; typedef struct { int st,ed,cost; }Edge; Edge edge[maxn*maxn]; int cmp(Edge a,Edge b){ return a.cost > b.cost; } int fa[maxn]; void init(int n){ for(int i=0;i<=maxn;i++) fa[i]=i; } int find(int x){ if(fa[x]==x) return fa[x]; else return fa[x]= find(fa[x]); } void Union(int x,int y){ int fx=find(x),fy=find(y); if(fx!=fy) fa[fx]=fy; } int kruskal(int n,int m){ sort(edge,edge+m,cmp); int rst=n; for(int i=0;i<m && rst >1;i++){ if(find(edge[i].st) != find(edge[i].ed)){ Union(edge[i].st,edge[i].ed); rst--; if(find(1) == find(n) ) return edge[i].cost; } } return -1; } int main(){ // freopen("in.txt","r",stdin); int T; scanf("%d",&T); for(int cas=1;cas <=T;cas ++){ int n,m; scanf("%d%d",&n,&m); int t1,t2,t3; for(int i=0;i<m;i++){ scanf("%d%d%d",&t1,&t2,&t3); edge[i].st=t1; edge[i].ed=t2; edge[i].cost =t3; } init(n); printf("Scenario #%d:/n",cas); printf("%d/n/n",kruskal(n,m) ); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 晋城| 高安市| 高碑店市| 宝兴县| 抚远县| 基隆市| 四川省| 内乡县| 峨边| 郴州市| 泸溪县| 永兴县| 格尔木市| 蕉岭县| 西乌珠穆沁旗| 大兴区| 呼伦贝尔市| 乾安县| 遵义县| 九龙县| 民权县| 东乌| 达孜县| 华坪县| 沁阳市| 永和县| 揭东县| 朔州市| 光山县| 石柱| 建瓯市| 蒲江县| 天峻县| 吴忠市| 锡林浩特市| 东至县| 尚志市| 台安县| 石首市| 安图县| 玉溪市|