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

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

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

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

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) ); }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌| 师宗县| 武穴市| 淳化县| 温泉县| 历史| 易门县| 阿拉善右旗| 登封市| 乡宁县| 昌图县| 明光市| 金沙县| 临武县| 黄梅县| 灵宝市| 新泰市| 马鞍山市| 丰城市| 南和县| 兴义市| 开原市| 天柱县| 吉林省| 三穗县| 湘乡市| 长春市| 临武县| 泗阳县| 凤庆县| 博野县| 北宁市| 青州市| 潜江市| 万荣县| 遂溪县| 景德镇市| 襄城县| 平陆县| 锦屏县| 朔州市|