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

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

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

2019-11-10 17:14:43
字體:
來源:轉載
供稿:網友

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變式,一種是最大生成樹,在最大生成樹路徑中找權值最小的邊。

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

#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 , 但是當起點與終點 在一個集合時要終止,否則會受到后續邊的影響。

#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) ); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平塘县| 广元市| 江安县| 和顺县| 江口县| 石首市| 罗田县| 河源市| 临安市| 怀仁县| 永靖县| 海兴县| 湟中县| 色达县| 马边| 特克斯县| 客服| 雷波县| 玉山县| 南通市| 南皮县| 翁源县| 肇源县| 文山县| 湖口县| 琼中| 广昌县| 雅江县| 元氏县| 磐安县| 南康市| 滦平县| 玉屏| 石屏县| 寿光市| 潼南县| 永和县| 双城市| 武陟县| 昌宁县| 泾阳县|