POJ1797 Heavy Transportation(dijkstra变形,spfa)

题目:

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 32753 Accepted: 8700

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

[Submit]   [Go Back]   [Status]   [Discuss]

题意:给了一个无向图,有n个城市,m条道路,给出了从一个到另一个的承载量,求从1--n的最大承载量

代码1(dijkstra):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 2010
#define inf 0x3f3f3f
using namespace std;
int n,m,maxx,u;
int map[maxnum][maxnum],dis[maxnum],vis[maxnum];//dis[n]表示从1--n的最大承载量
int main()
{
    int t,q=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        mem(map,0);
        int a,b,c;
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            map[a][b]=map[b][a]=c;
        }
        //dijikstra
        for(int i=1; i<=n; i++)//初始化
        {
            vis[i]=0;
            dis[i]=map[1][i];
        }

        vis[1]=1;//标记点1已经访问过
        for(int i=1; i<=n-1; i++)
        {
            maxx=0;
            for(int j=1; j<=n; j++)
            {
                if(!vis[j]&&dis[j]>maxx)
                {
                    maxx=dis[j];
                    u=j;
                }
            }
            vis[u]=1;
            for(int j=1; j<=n; j++)
                if(!vis[j]&&dis[j]<min(dis[u],map[u][j]))
                    dis[j]=min(dis[u],map[u][j]);
        }
        printf("Scenario #%d:\n%d\n\n",q++,dis[n]);//注意输出格式
    }
    return 0;
}
代码2(spfa):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxnum 1010
#define inf 0x3f3f3f
using namespace std;
int n,m,maxx,first[maxnum],next[100010],num;
int dis[maxnum],vis[maxnum];//dis[n]表示从1--n的最大承载量
struct node
{
    int u, v, w;
} G[100010];
void add_map(int u,int v,int w)//邻接表存图
{
    G[num].u=u,G[num].v=v,G[num].w=w;
    next[num]=first[u];
    first[u]=num++;
};
int main()
{
    int t,T=1;
    scanf("%d",&t);
    while(t--)
    {
        int k;
        scanf("%d%d",&n,&m);
        mem(first,-1);
        int u,v,w;
        num=1;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add_map(u,v,w);
            add_map(v,u,w);//无向图,存两遍
        }
        //spfa
        mem(dis,0);
        mem(vis,0);
        vis[1]=1;
        dis[1]=inf;//把初始的路标记为无穷大
        queue<int>q;
        q.push(1);
        while(!q.empty())
        {
            k=q.front();
            q.pop();
            vis[k]=0;
            for(int i=first[k]; i!=-1; i=next[i])
            {
                if(dis[G[i].v]<min(dis[k],G[i].w))//如果从1到当前点的承载量小于从1--k,与当前的第i条边的承载量的最小值
                {
                    dis[G[i].v]=min(dis[k],G[i].w);
                    if(!vis[G[i].v])
                    {
                        vis[G[i].v]=1;
                        q.push(G[i].v);
                    }
                }
            }
        }
        printf("Scenario #%d:\n%d\n\n",T++,dis[n]);
    }
    return 0;
}



#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 10000+20
#define M 1000000+10
#define LL long long
using namespace std;
int n,m,len;
struct node
{
    int v,w,next;
} G[M];
int first[N],dis[N],vis[N];

void add(int u,int v,int w)
{
    G[len].v=v,G[len].w=w;
    G[len].next=first[u];
    first[u]=len++;
}
void spfa()
{
    for(int i=1; i<=n; i++)
    {
        dis[i]=-inf;
        vis[i]=0;
    }
    dis[1]=inf,vis[1]=1;
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int start=q.front();
        q.pop();
        vis[start]=0;
        for(int i=first[start]; i!=-1; i=G[i].next)
        {
            int v=G[i].v,w=G[i].w;
            if(dis[v]<min(dis[start],w))
            {
                dis[v]=min(dis[start],w);
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int main()
{
    int t,q=1;
    scanf("%d",&t);
    while(t--)
    {
        mem(first,-1);
        scanf("%d%d",&n,&m);
        len=0;
        int u,v,w;
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        spfa();
        printf("Scenario #%d:\n%d\n\n",q++,dis[n]);
    }
    return 0;
}


好长时间不写题,感觉脑子都不够用了,还是要经常练练啊。。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值