zoukankan      html  css  js  c++  java
  • POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    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

    Http

    POJ:https://vjudge.net/problem/POJ-1797
    SCU:https://vjudge.net/problem/SCU-1819

    Source

    图论,最短路径

    题目大意

    求解两点之间所有路径中,最小权值最大的路径

    解决思路

    同样运用改进的spfa算法解决,顺带复习了一下Dijkstra算法
    Dijkstra(250ms)略快于spfa(266ms)
    请不要使用cin读入,并且注意输出格式

    代码

    spfa实现

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    
    const int maxN=1001;
    const int inf=2147483647;
    
    class Edge
    {
    public:
        int v,w;
    };
    
    int n,m;
    vector<Edge> E[maxN];
    int Dist[maxN];
    bool inqueue[maxN];
    queue<int> Q;
    
    int main()
    {
        int T;
        cin>>T;
        for (int ti=1;ti<=T;ti++)
        {
            scanf("%d%d",&n,&m);
            for (int i=1;i<=n;i++)
                E[i].clear();
            for (int i=1;i<=m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                E[u].push_back((Edge){v,w});
                E[v].push_back((Edge){u,w});
            }
            memset(Dist,0,sizeof(Dist));
            memset(inqueue,0,sizeof(inqueue));
            while (!Q.empty())
                Q.pop();
            inqueue[1]=1;
            Q.push(1);
            Dist[1]=inf;
            do
            {
                int u=Q.front();
                Q.pop();
                inqueue[u]=0;
                for (int i=0;i<E[u].size();i++)
                {
                    int v=E[u][i].v;
                    if (min(Dist[u],E[u][i].w)>Dist[v])
                    {
                        Dist[v]=min(Dist[u],E[u][i].w);
                        if (inqueue[v]==0)
                        {
                            Q.push(v);
                            inqueue[v]=1;
                        }
                    }
                }
            }
            while (!Q.empty());
            printf("Scenario #%d:
    %d
    
    ",ti,Dist[n]);
        }
        return 0;
    }
    

    Dijkstra实现

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    
    const int maxN=1001;
    const int inf=2147483647;
    
    class Edge
    {
    public:
        int v,w;
    };
    
    int n,m;
    vector<Edge> E[maxN];
    int Dist[maxN];
    bool vis[maxN];
    
    int main()
    {
        int T;
        cin>>T;
        for (int ti=1;ti<=T;ti++)
        {
            scanf("%d%d",&n,&m);
            for (int i=1;i<=n;i++)
                E[i].clear();
            for (int i=1;i<=m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
    
                E[u].push_back((Edge){v,w});
                E[v].push_back((Edge){u,w});
            }
            memset(Dist,0,sizeof(Dist));
            memset(vis,0,sizeof(vis));
            Dist[1]=inf;
            for (int i=1;i<n;i++)
            {
                int id,mx=-inf;
                for (int j=1;j<=n;j++)
                    if ((Dist[j]>mx)&&(vis[j]==0))
                    {
                        mx=Dist[j];
                        id=j;
                    }
                if (id==n)
                    break;
                vis[id]=1;
                for (int j=0;j<E[id].size();j++)
                {
                    int v=E[id][j].v;
                    if ((vis[v]==0)&&(min(Dist[id],E[id][j].w)>Dist[v]))
                    {
                        Dist[v]=min(Dist[id],E[id][j].w);
                    }
                }
            }
            printf("Scenario #%d:
    %d
    
    ",ti,Dist[n]);
        }
        return 0;
    }
    
  • 相关阅读:
    Linux rm命令详解
    标准时间格式("%Y-%m-%dT%H:%M:%S")转化(基于python 3.6)
    通过load json文件读取json指定数据(基于python 3.6)
    遍历win10文件夹并解析json文件,按照json格式存入mongo数据库(基于python 3.6)
    mongo的备份数据库导入现有数据库
    python 获取网页内容新增网页分类+删除指定后缀数组元素功能(基于python 3.6)
    sqlite3的安装和使用(基于python3.5)
    python 获取提交表单网址内容(即需要密码网址)以财务网站为例
    python 分析PDF文件 (基于使用pdf2htmlEX.exe python3.6)
    python 复制多个文件到指定目录(基于python 3.X)
  • 原文地址:https://www.cnblogs.com/SYCstudio/p/7225128.html
Copyright © 2011-2022 走看看