24h購物| | PChome| 登入
2012-12-26 19:19:32| 人氣622| 回應0 | 上一篇 | 下一篇

[UVA][spfa] 11280 - Flying to Fredericton

推薦 0 收藏 0 轉貼0 訂閱站台

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 <stdio.h>
#include <map>
#include <queue>
#include <iostream>
#define oo 0xfffffff
using namespace std;
struct arc {
int to, v;
arc(int x, int y) : to(x), v(y) {}
};
vector<arc> g[105];
int inq[105][105];
void spfa(int n, int k) {
queue<arc> Q;
arc tn(0,0);
int dist[105][k+1], i, j;
for(i = 0; i < n; i++)
for(j = 0; j <= k; j++)
dist[i][j] = oo, inq[i][j] = 0;
dist[0][0] = 0;
Q.push(arc(0,0));
while(!Q.empty()) {
tn = Q.front();
Q.pop();
if(tn.v >= k) continue;
inq[tn.to][tn.v] = 0;
for(vector<arc>::iterator it = g[tn.to].begin();
it != g[tn.to].end(); it++) {
if(dist[it->to][tn.v+1] > dist[tn.to][tn.v] + it->v) {
dist[it->to][tn.v+1] = dist[tn.to][tn.v] + it->v;
if(!inq[it->to][tn.v+1]) {
inq[it->to][tn.v+1] = 1;
Q.push(arc(it->to, tn.v+1));
}
}
}
}
int ans = oo;
for(i = 0; i <= k; i++)
ans = ans < dist[n-1][i] ? ans : dist[n-1][i];
if(ans == oo)
puts("No satisfactory flights");
else
printf("Total cost of flight(s) is $%d\n", ans);
}
int main() {
int cases = 0, n, m, i, j, v;
char buf[105];
scanf("%*d");
while(scanf("%d", &n) == 1) {
map<string, int> R;
for(i = 0; i < n; i++) {
scanf("%s", buf);
R[buf] = i;
g[i].clear();
}
scanf("%d", &m);
while(m--) {
scanf("%s", buf);
i = R[buf];
scanf("%s", buf);
j = R[buf];
scanf("%d", &v);
g[i].push_back(arc(j, v));
}
if(cases) puts("");
printf("Scenario #%d\n", ++cases);
int q, l;
scanf("%d", &q);
while(q--) {
scanf("%d", &l);
if(l < 0) l = 0;
spfa(n, l+1);
}
}
return 0;
}

台長: Morris
人氣(622) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][bfs] 11624 - Fire!
此分類上一篇:[UVA] 11804 - Argentina

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文