Poj 1797 Heavy Transportation【Dijkstra变形】

来源:互联网 发布:网络创业的机会来源 编辑:程序博客网 时间:2024/05/21 06:34

Heavy Transportation

Time Limit: 3000MS

 

Memory Limit: 30000K

Total Submissions: 28249

 

Accepted: 7548

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

Source

TUD Programming Contest 2004, Darmstadt, Germany

 

题目大意:首先输入一个整数t,表示测试 数据组数,接下来每一组第一行输入两个整数n,m。表示一共有n个节点,m条边(不明白为什么是无向边,写有向边就是Wa,无向边就是Ac),表示从节点a,到节点b最大载货量为c。问从节点1出发,选择一条路径,到达n,使得能够送得最大的货量为多少。


思路:

1、首先初始化dis【i】=0;

2、每一次选择dis【i】最大的节点(当然是没有选过的),向外拓展,此点记做u。

3、枚举选取到的点的能够到达的点,记做v,其路径权值为w,那么不难推出松弛式:dis【v】=max(dis【v】,tmp);tmp=min(dis【u】,w)【dis【u】!=0】;tmp=w【dis【u】==0】;

4、将所有点都选取过了之后,输出dis【n】;


AC代码:


#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int head[100000];struct EdgeNode{    int to;    int w;    int next;}e[1000000];int dis[100000];int vis[100000];int n,m,cont;void add(int from,int to,int w){    e[cont].to=to;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void Dij(){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)dis[i]=0;    for(int i=1;i<=n;i++)    {        int k;        int tmp=-1;        for(int j=1;j<=n;j++)        {            if(vis[j]==0&&dis[j]>tmp)            {                k=j;                tmp=dis[j];            }        }        vis[k]=1;        for(int j=head[k];j!=-1;j=e[j].next)        {            int v=e[j].to;            int w=e[j].w;            if(vis[v]==0)            {                int tt=min(dis[k],w);                if(tt==0)tt=w;                dis[v]=max(dis[v],tt);            }            else if(v==k&&n==1)//解决n==1时候可能存在的自环问题            {                int tt=min(dis[k],w);                if(tt==0)tt=w;                dis[v]=max(dis[v],tt);            }        }    }}int main(){    int kase=0;    int t;    scanf("%d",&t);    while(t--)    {        cont=0;        memset(head,-1,sizeof(head));        scanf("%d%d",&n,&m);        for(int i=0;i<m;i++)        {            int x,y,w;            scanf("%d%d%d",&x,&y,&w);            add(x,y,w);            //add(y,x,w);        }        Dij();        printf("Scenario #%d:\n",++kase);        printf("%d\n",dis[n]);        printf("\n");    }}/*55 61 3 43 5 73 5 71 5 91 2 32 5 100*/







0 0
原创粉丝点击