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) ); }}新聞熱點
疑難解答