zoukankan      html  css  js  c++  java
  • UVA 11280 Flying to Fredericton 最短路DP

    Problem C

    FLYING TO FREDERICTON

    After being inspired by Salvador Dali's artwork, Brett decided he would like to travel to Fredericton, New Brunswick to visit the Beaverbrook Art Gallery.

    Brett lives in Calgary, and would like to find the cheapest flight or flights that would take him to Fredericton. He knows that a direct flight from Calgary to Fredericton, if one exists, would be absurdly expensive, so he is willing to tolerate a certain number of stopovers. However, there are multiple airlines in Canada with so many different flights between different cities now, which makes it very difficult for Brett to find the least expensive way to Fredericton! Can you write a program to help Brett plan his route?

    Map showing a sample of the flights that would take Brett to Fredericton.

    You will be given a list of cities between and including Calgary and Fredericton. The cities will be given in order of "nearest" to "farthest". The first city will always be "Calgary" and the last "Fredericton".

    You will also be given a list of flights between pairs of cities, and the associated cost for each flight, taxes included. There will never be a flight from a farther city to a nearer city - Brett has already discarded those flights, deeming them to be a waste of time and money. Bear in mind, however, that there may be more than one flight between any two cities, as Brett is considering flights from all airlines.

    Finally, you are presented with a number of queries. Each query is a single integer indicating the maximum number of stopovers Brett will tolerate. For each query, your program must calculate the least total cost of flights that would take Brett from Calgary to Fredericton with no more than the requested number of stopovers.

    Input

    The first line of input contains a single number indicating the number of scenarios to process. A blank line precedes each scenario.

    Each scenario begins with a number N (2 ≤ N ≤ 100), the number of cities, followed by N lines containing the names of the cities. A city name is a string of up to 20 uppercase and lowercase letters. Next is a number M (0 ≤ M ≤ 1000), the number of flights available, followed by M lines describing the flights. Each flight is described by its departure city, its destination city, and an integer representing its cost in dollars. The final line starts with Q (1 ≤ Q ≤ 10), the number of queries, followed by Q numbers indicating the maximum number of stopovers.

    Output

    For each scenario, your program should output the scenario number, followed by the least total cost of the flights for each query. Follow the format of the sample output. If no flights can satisfy a query, write "No satisfactory flights". Output a blank line between scenarios.

    Sample Input

    2
    
    4
    Calgary
    Winnipeg
    Ottawa
    Fredericton
    6
    Calgary Winnipeg 125
    Calgary Ottawa 300
    Winnipeg Fredericton 325
    Winnipeg Ottawa 100
    Calgary Fredericton 875
    Ottawa Fredericton 175
    3 2 1 0
    
    3
    Calgary
    Montreal
    Fredericton
    2
    Calgary Montreal 300
    Montreal Fredericton 325
    1 0
    

    Output for the Sample Input

    Scenario #1
    Total cost of flight(s) is $400
    Total cost of flight(s) is $450
    Total cost of flight(s) is $875
    
    Scenario #2
    No satisfactory flights
    ---------------

    占坑

    ---------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <algorithm>
    #include <queue>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int maxn=211;
    const int maxm=2111;
    map<string,int>mp;
    char name[111111];
    int S,T;
    
    
    struct EdgeNode{
        int to;
        int w;
        int next;
    };
    struct BellmanFord{
        EdgeNode edges[maxm];
        int head[maxn],edge,n;
        bool inq[maxn][maxn];
        queue< pair<int,int> >que;
        int dis[maxn][maxn];
        void addedge(int u,int v,int c){
            edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        }
        void init(int n){
            memset(head,-1,sizeof(head));
            edge=0;
            this->n=n;
        }
        bool spfa(int s,int len){
            int u;
            memset(dis,INF,sizeof(dis));
            memset(inq,0,sizeof(inq));
            while (!que.empty()) que.pop();
            dis[0][s]=0;
            inq[0][s]=true;
            que.push( make_pair(0,s) );
            while (!que.empty()){
                int tim=que.front().first;
                u=que.front().second;
                que.pop();
                inq[tim][u]=false;
                for (int i=head[u];i!=-1;i=edges[i].next){
                    int v=edges[i].to;
                    int w=edges[i].w;
                    if ( tim+1<=len&&dis[tim+1][v]>dis[tim][u]+w ){
                        dis[tim+1][v]=dis[tim][u]+w;
                        //do something
    
                        //nice try
                        if (!inq[tim+1][v]){
                            inq[tim+1][v]=true;
                            que.push( make_pair(tim+1,v) );
                        }
                    }
                }
            }
            return true;
        }
    }solver;
    
    
    int n,m;
    int main()
    {
        int TMD;
        int cas=0;
        scanf("%d",&TMD);
        while (TMD--){
            //input()
            scanf("%d",&n);
            solver.init(n);
            for (int i=1;i<=n;i++){
                scanf("%s",name);
                mp[name]=i;
            }
            scanf("%d",&m);
            for (int i=0;i<m;i++){
                int x,y,c;
                scanf("%s",name);
                x=mp[name];
                scanf("%s",name);
                y=mp[name];
                scanf("%d",&c);
                solver.addedge(x,y,c);
            }
            S=mp["Calgary"];
            T=mp["Fredericton"];
            //Solve()
            int Q,qst;
            printf("Scenario #%d
    ",++cas);
            scanf("%d",&qst);
            while (qst--){
                scanf("%d",&Q);
                Q++;
                if (Q>n) Q=n;
                if (Q<1) Q=1;
                solver.spfa(S,Q);
                int ans=INF;
                for(int i=0;i<=Q;i++)
                    ans=min(ans,solver.dis[i][T]);
                if (ans==INF){
                    printf("No satisfactory flights
    ");
                }else{
                    printf("Total cost of flight(s) is $%d
    ",ans);
                }
            }
            if (TMD!=0) puts("");
        }
        return 0;
    }
    







  • 相关阅读:
    制作OSGB数据索引
    汤臣一品
    Python 库/模块的安装、查看
    ezdxf包下autocad的开发
    python3.7安装pylint
    航拍全景图补天
    电脑百科
    使用Excel批量提取文件名
    利用爬虫实现网上的图片自动下载
    MarkDown&思维导图
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681575.html
Copyright © 2011-2022 走看看