zoukankan      html  css  js  c++  java
  • uva 11280 最短路

    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
    

    Brett Shikaze
    Calgary Collegiate Programming Contest 2006

    WA了N次,参考了众神题解。

    限制边数,求最短路。

    思路:暴力 + 最短路;开二位数组,比如d[i][j]表示点i出发到达点i时经过了j个点的最短路值, 则首先dijkstra打出这张表,然后输出

    min{d[destination][j]} j<=limitation 即可。

    写了三个版本,网上的大都是第三个,而且dijkstra的效率居然没Bellman-Ford高。。。

    1.Dijkstra

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define INF 100000000
    #define MAXN 111
    #define MAXM 2999
    
    struct edge{
        int u, v, w, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m, cnt;
    
    map<string, int> mp;
    int ID(string str){
        if(mp.count(str)) return mp[str];
        return mp[str]=cnt++;
    }
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
    }
    
    struct node{
        int u, num, d;
        bool operator < (const node &rhs)const{
            return d > rhs.d;
        }
    };
    
    int d[MAXN][MAXN];
    bool done[MAXN][MAXN];
    
    void Dijkstra(cint s, cint t){
        priority_queue<node> q;
        for(int i=0; i<n; i++){
            fill_n(d[i]+1, n, INF);
            fill_n(done[i]+1, n, false);
        }
        d[s][1]=0;
        q.push((node){s, 1, 0});
        while(!q.empty()){
            node ut = q.top();  q.pop();
            int u = ut.u, num = ut.num;
            if(done[u][num]) continue;      done[u][num]=true;
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v = e[i].v, w = e[i].w;
                if(d[v][num+1]>d[u][num]+w){
                    d[v][num+1] = d[u][num] + w;
                    q.push((node){v, num+1, d[v][num+1]});
                }
            }
        }
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T, kase=1;
        scanf(" %d", &T);
        while(T--){
            int i, w, q;
            char s[33], des[33];
            scanf(" %d", &n);
            cnt=0;          mp.clear();
            for(i=0; i<n; i++){
                scanf(" %s", s);    ID(string(s));
            }
            scanf(" %d", &m);
            fill_n(h, n, -1);       cc=0;
            while(m--){
                scanf(" %s %s %d", s, des, &w);
                add(ID(string(s)), ID(string(des)), w);
            }
            Dijkstra(0, n-1);
    
            scanf(" %d", &q);
            if(kase>1) printf("
    ");
            printf("Scenario #%d
    ", kase++);
            while(q--){
                scanf(" %d", &w);   w+=2;
                w = min(w, n);              //这里很容易WA,当然也和d数组的初始化有关
                int ans = *min_element(d[n-1]+2, d[n-1]+w+1);
                if(ans >= INF) printf("No satisfactory flights
    ");
                else printf("Total cost of flight(s) is $%d
    ", ans);
            }
        }
        return 0;
    }
    

     2.Bellman-Ford

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define INF 100000000
    #define MAXN 111
    #define MAXM 2999
    
    struct edge{
        int u, v, w, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m, cnt;
    
    map<string, int> mp;
    int ID(string str){
        if(mp.count(str)) return mp[str];
        return mp[str]=cnt++;
    }
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
    }
    
    struct node{
        int u, num;
    };
    
    int d[MAXN][MAXN];
    bool inq[MAXN][MAXN];
    
    void bford(cint s, cint t){
        queue<node> q;
        for(int i=0; i<n; i++){
            fill_n(d[i]+1, n, INF);
            fill_n(inq[i]+1, n, false);
        }
        d[s][1]=0;      inq[s][1]=true;
        q.push((node){s, 1});
        while(!q.empty()){
            node ut = q.front();  q.pop();
            int u = ut.u, num = ut.num;
            inq[u][num] = false;
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v = e[i].v, w = e[i].w;
                if(d[v][num+1]>d[u][num]+w){
                    d[v][num+1] = d[u][num] + w;
                    if(!inq[v][num+1])
                        q.push((node){v, num+1}),
                        inq[v][num+1]=true;
                }
            }
        }
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T, kase=1;
        scanf(" %d", &T);
        while(T--){
            int i, w, q;
            char s[33], des[33];
            scanf(" %d", &n);
            cnt=0;          mp.clear();
            for(i=0; i<n; i++){
                scanf(" %s", s);    ID(string(s));
            }
            scanf(" %d", &m);
            fill_n(h, n, -1);       cc=0;
            while(m--){
                scanf(" %s %s %d", s, des, &w);
                add(ID(string(s)), ID(string(des)), w);
            }
            bford(0, n-1);
    
            scanf(" %d", &q);
            if(kase>1) printf("
    ");
            printf("Scenario #%d
    ", kase++);
            while(q--){
                scanf(" %d", &w);   w+=2;
                w = min(w, n);
                int ans = *min_element(d[n-1]+2, d[n-1]+w+1);
                if(ans >= INF) printf("No satisfactory flights
    ");
                else printf("Total cost of flight(s) is $%d
    ", ans);
            }
        }
        return 0;
    }
    

     3.还是Bellman-Ford

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define INF 100000000
    #define MAXN 111
    #define MAXM 2999
    
    struct edge{
        int u, v, w, nxt;
    }e[MAXM];
    int h[MAXN], cc, n, m, cnt;
    
    map<string, int> mp;
    int ID(string str){
        if(mp.count(str)) return mp[str];
        return mp[str]=cnt++;
    }
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
    }
    
    struct node{
        int u, num;
    };
    
    int d[MAXN][MAXN];
    bool inq[MAXN][MAXN];
    
    void bford(cint s, cint t, cint len){
        queue<node> q;
        for(int i=0; i<n; i++){
            fill_n(d[i]+1, n, INF);
            fill_n(inq[i]+1, n, false);
        }
        d[s][1]=0;      inq[s][1]=true;
        q.push((node){s, 1});
        while(!q.empty()){
            node ut = q.front();  q.pop();
            int u = ut.u, num = ut.num;
            inq[u][num] = false;
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v = e[i].v, w = e[i].w;
                if(d[v][num+1]>d[u][num]+w && num+1<=len){
                    d[v][num+1] = d[u][num] + w;
                    if(!inq[v][num+1])
                        q.push((node){v, num+1}),
                        inq[v][num+1]=true;
                }
            }
        }
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T, kase=1;
        scanf(" %d", &T);
        while(T--){
            int i, w, q;
            char s[33], des[33];
            scanf(" %d", &n);
            cnt=0;          mp.clear();
            for(i=0; i<n; i++){
                scanf(" %s", s);    ID(string(s));
            }
            scanf(" %d", &m);
            fill_n(h, n, -1);       cc=0;
            while(m--){
                scanf(" %s %s %d", s, des, &w);
                add(ID(string(s)), ID(string(des)), w);
            }
            scanf(" %d", &q);
            if(kase>1) printf("
    ");
            printf("Scenario #%d
    ", kase++);
            while(q--){
                scanf(" %d", &w);   w+=2;
                w = min(w, n);
                bford(0, n-1, w);
                int ans = *min_element(d[n-1]+2, d[n-1]+w+1);
                if(ans >= INF) printf("No satisfactory flights
    ");
                else printf("Total cost of flight(s) is $%d
    ", ans);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Python学习笔记
    Python学习笔记
    不定宽高垂直居中分析
    PhoneGap安装配置
    Mongoose学习参考资料
    我的node+express小例子
    node+express+mongodb初体验
    stylie工具轻松搞定css3抛物线动画
    fis前端开发框架
    Fiddler实现手机的抓包(转载园友小坦克)
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3377200.html
Copyright © 2011-2022 走看看