POJ1797:Heavy Transportation(改造Dijkstra)
Heavy Transportation
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 52728 | Accepted: 13474 |
題目鏈接:http://poj.org/problem?id=1797
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.
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號點到n號點所經路徑的最小邊權的最大值為多少。
題解:
其實本題也不是嚴格的dijkstra算法,只是利用了類似的貪心思想。
我們首先維護一個到當前點所有路徑中的最小值,把它扔進優先隊列裏面,從優先隊列裏面每次取大值出來去更新與之相鄰的點。
這裏的正確性證明和dijkstra算法的證明類似,也就是說一個點去更新其它點後,不會被其他點又一次更新。
代碼如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <queue> #define INF 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 1005; int T; int n,tot,m; int head[N],vis[N],d[N]; struct Edge{ int u,v,next,w; }e[N*N<<1]; struct node{ int d,u; bool operator < (const node &A)const{ return d<A.d; } }; void adde(int u,int v,int w){ e[tot].v=v;e[tot].next=head[u];e[tot].w=w;head[u]=tot++; } void Dijkstra(int s){ priority_queue <node> q; memset(d,0,sizeof(d));memset(vis,0,sizeof(vis)); node now;d[s]=INF; now.d=INF;now.u=s; q.push(now); while(!q.empty()){ node cur = q.top();q.pop(); int u=cur.u; if(vis[u]) continue ; vis[u]=1; for(int i=head[u];i!=-1;i=e[i].next){ int v=e[i].v; if(d[v]<min(d[u],e[i].w)){ d[v]=min(d[u],e[i].w); now.d=d[v];now.u=v; q.push(now); } } } } int main(){ cin>>T; int cnt = 0,first=1; while(T--){ cnt++; scanf("%d%d",&n,&m); memset(head,-1,sizeof(head));tot=0; for(int i=1;i<=m;i++){ int u,v,c; scanf("%d%d%d",&u,&v,&c); adde(u,v,c);adde(v,u,c); } Dijkstra(1); printf("Scenario #%d:\n",cnt); cout<<d[n]<<endl; cout<<endl; } return 0; }
POJ1797:Heavy Transportation(改造Dijkstra)