24h購物| | PChome| 登入
2013-07-10 07:02:08| 人氣715| 回應0 | 上一篇 | 下一篇

[UVA][最短路] 11377 - Airport Setup

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

Problem A

Airport Setup

Time Limit: 3 Second

 

 

There are N cities in the kingdom ‘Nameless’ and among them K cities have airport. There the airline companies are willing to setup their flight directly between M pairs of the cities. They think that it will not be profitable for them to run flights between other pairs of cities. Note that if there are airports in both of the cities of any of their M pairs only then they will run flight between that pair otherwise not. Because of this policy of the airline companies and also because all the cities don’t have airport many people can’t travel between their desired cities. So some people of the country have demanded that the king should do something so that they can travel to their destinations. There are Q demands which came to the king. Every demand is a pair (x, y) which means some people want a route between city x and city y. The king really wants to satisfy the demands but he does not want to setup too many airports. So for every demand (x, y) the king wants to know the minimum number of airports he needs to setup to establish a route between city x and city y.

 

Input:

The first line of the input contains X the number of test cases which is at most 10. Each case starts with three numbers N ( 1 ≤ N ≤ 2,000), M ( 0 ≤ M ≤ 10,000) and K ( 1 ≤ K ≤ N). Here N is the number of cities, M is the number of direct air routes the airlines companies are willing to setup and K is the number of cities which has airport. Then there will be a line containing K integers which denote the name of the cities which have airport (city name ranges from 1 to N). Each city is separated by a single space. Then there will be M lines each of which contains one pair “a b”. It means that the airline companies are willing to run their flights between city a and b. The next line contains Q ( 1 ≤ Q ≤ 50 ) which is the number of demands came to the king. Then there will be Q lines each containing a pair “x y” which means that the king should build minimum airport to establish a route between city x and city y. There is a blank line between every consecutive test case.

 

Output:

For every test case you have to output the case number first (See the sample). Then you have to output Q lines each containing the minimum number of airports the king has to setup to satisfy that demand. If it’s impossible to satisfy that demand then output -1. You have to output a blank line after each test case.

 

SAMPLE INPUT

OUTPUT FOR SAMPLE INPUT

1

6 4 4

1 2 5 6

1 2

3 5

2 4

4 5

3

1 2

1 3

1 6

 

Case 1:

0

2

-1

 

 

 

Problemsetter: Tasnim Imran Sunny

Special Thanks To: Md. Mahbubul Hasan



題目給定一張圖,有些點上已經有機場了,然後以及數條即將興建的航班起點與終點。

但現在只需要根據民眾的要求建造機場就可以,因此對於一組要求起點到終點,

求最少建造機場的數量,如果起點跟終點一樣輸出 "0",如果起點沒有機場也是要興建的。

#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
vector<int> g[2005];
int airport[2005];
struct E {
    int node, v;
    E(int a, int b):
        node(a), v(b) {}
    bool operator<(const E &A) const {
        return v > A.v;
    }
};
void dijkstra(int st, int ed, int n) {
    if(st == ed) {
        puts("0");
        return;
    }
    priority_queue<E> pQ;
    E tn(0,0);
    int dist[n+1], i;
#define oo 0xffffff
    for(i = 0; i <= n; i++)
        dist[i] = oo;
    dist[st] = (airport[st] == 0);
    pQ.push(E(st, 0));
    while(!pQ.empty()) {
        tn = pQ.top(), pQ.pop();
        if(tn.v >= dist[ed])  continue;
        for(vector<int>::iterator it = g[tn.node].begin();
            it != g[tn.node].end(); it++) {
            if(airport[*it] == 1) {
                if(dist[*it] > dist[tn.node]) {
                    dist[*it] = dist[tn.node];
                    pQ.push(E(*it, dist[*it]));
                }
            } else {
                if(dist[*it] > dist[tn.node]+1) {
                    dist[*it] = dist[tn.node]+1;
                    pQ.push(E(*it, dist[*it]));
                }
            }
        }
    }
    if(dist[ed] == oo)
        puts("-1");
    else
        printf("%d\n", dist[ed]);
}
int main() {
    int testcase, cases = 0;
    int i, j, k, n, m, q;
    int x, y;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d %d", &n, &m, &k);
        for(i = 1; i <= n; i++)
            g[i].clear(), airport[i] = 0;
        while(k--) {
            scanf("%d", &x);
            airport[x] = 1;
        }
        while(m--) {
            scanf("%d %d", &x, &y);
            g[x].push_back(y);
            g[y].push_back(x);
        }
        scanf("%d", &q);
        printf("Case %d:\n", ++cases);
        while(q--) {
            scanf("%d %d", &x, &y);
            dijkstra(x, y, n);
        }
        puts("");
    }
    return 0;
}

台長: Morris
人氣(715) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11326 - Laser Pointer
此分類上一篇:[UVA][math] 11300 - Spreading the Wealth

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