UVA - 11280 Flying to Fredericton(二維SPFA)

來源:互聯網
上載者:User


Link:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25133



Description 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

24CalgaryWinnipegOttawaFredericton6Calgary Winnipeg 125Calgary Ottawa 300Winnipeg Fredericton 325Winnipeg Ottawa 100Calgary Fredericton 875Ottawa Fredericton 1753 2 1 03CalgaryMontrealFredericton2Calgary Montreal 300Montreal Fredericton 3251 0
Output for the Sample Input
Scenario #1Total cost of flight(s) is $400Total cost of flight(s) is $450Total cost of flight(s) is $875Scenario #2No satisfactory flights
Brett Shikaze
Calgary Collegiate Programming Contest 2006


題意:給出n個城市和m條航線,每條航線串連兩個城市,一個是航線的出發城市,一個是航線的抵達城市,並且有相應的花費。給出起點和目的地城市,然後有Q個詢問,每個詢問給出在起點和目的地城市路線中間最大可經過的城市數目,問在中間經過的城市數要小於等於最大可經過的城市數目的條件下,從指定的該起點城市到目的地城市的花費最少是多少。如果在該條件限制下不能抵達目的地,輸出“No satisfactory flights”。


解題思路:要用二維來表示狀態。一維表示到達某個城市的最短路徑(也就是題目中的花費),第二維表示到達某個城市時中間經過的城市數目。同時該題輸入資料比較麻煩,要用一些技巧對輸入的資料進行處理,還要注意查詢時輸入資料可能不是合法的,這個也是一大坑點,要對資料判斷後處理。剩下的就是裸的SPFA了。


AC code:

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<map>#include<cmath>#define LL long long#define MAXN 1000010using namespace std;const int INF=0x3f3f3f3f;int n,m,tot;int head[MAXN];struct state{//用結構體來表示當前的狀態:當前到達的城市編號為city,經過停留的城市數為stop int city;int stop;};struct node{int from;int to;int w;int next;}edge[MAXN];int dis[111][111];bool inq[111][111];int cnt[111][111];void init(){memset(head,-1,sizeof(head));tot=0;}void add(int from,int to,int w){edge[tot].from=from;edge[tot].to=to;edge[tot].w=w;edge[tot].next=head[from];head[from]=tot++;}bool spfa(int start,int ed){deque<state>q;memset(dis,INF,sizeof(dis));memset(inq,false,sizeof(inq));memset(cnt,0,sizeof(cnt));state st;st.city=start;st.stop=0;dis[st.city][st.stop]=0;q.push_back(st);inq[st.city][st.stop]=true;cnt[st.city][st.stop]++;while(!q.empty()){state now=q.front();q.pop_front();inq[now.city][now.stop]=false;if(cnt[now.city][now.stop]>n){continue;}if(cnt[now.city][now.stop]==n){dis[now.city][now.stop]=INF;}for(int i=head[now.city];i!=-1;i=edge[i].next){state nex;nex.city=edge[i].to;nex.stop=now.stop+1;if(dis[nex.city][nex.stop]>dis[now.city][now.stop]+edge[i].w){dis[nex.city][nex.stop]=dis[now.city][now.stop]+edge[i].w;if(!inq[nex.city][nex.stop]){state front=q.front();if(!q.empty()&&dis[nex.city][nex.stop]<dis[front.city][front.stop]){q.push_front(nex);}else{q.push_back(nex);}}}}}return true;}int main(){//freopen("D:\in.txt","r",stdin);int T,cas,i,j,u,v,w,Q,s,ans;scanf("%d",&T);for(cas=1;cas<=T;cas++){scanf("%d",&n);map<string,int>city;string name1,name2;for(i=1;i<=n;i++){cin>>name1;//一大坑點,輸入的不是城市編號,是城市名,要用map巧妙地city[name1]=i;//將字串的城市名轉化為數實值型別的城市編號  }scanf("%d",&m);init();while(m--){cin>>name1>>name2>>w;add(city[name1],city[name2],w);}int start=city["Calgary"];int ed=city["Fredericton"];spfa(start,ed);scanf("%d",&Q);printf("Scenario #%d\n",cas);while(Q--){scanf("%d",&s);s=min(s,n-2);//一大坑點。。。輸入的可停留次數可能超出合法範圍,必須自己做處理。。。 ans=INF;for(i=0;i<=s;i++)ans=min(ans,dis[n][i+1]);if(ans>=INF){printf("No satisfactory flights\n");}else{printf("Total cost of flight(s) is $%d\n",ans);}}if(cas!=T){printf("\n");}}return 0; } 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.