SDNU 1139.Emergency(起点更改最短路问题)

Description

Kudo’s real name is not Kudo. Her name is Kudryavka Anatolyevna Strugatskia, and Kudo is only her nickname.
Now, she is facing an emergency in her hometown:
Her mother is developing a new kind of spacecraft. This plan costs enormous energy but finally failed. What’s more, because of the failed project, the government doesn’t have enough resource take measure to the rising sea levels caused by global warming, lead to an island flooded by the sea.
Dissatisfied with her mother’s spacecraft and the government, civil war has broken out. The foe wants to arrest the spacecraft project’s participants and the “Chief criminal” – Kudo’s mother – Doctor T’s family.
At the beginning of the war, all the cities are occupied by the foe. But as time goes by, the cities recaptured one by one.
To prevent from the foe’s arrest and boost morale, Kudo and some other people have to distract from a city to another. Although they can use some other means to transport, the most convenient way is using the inter-city roads. Assuming the city as a node and an inter-city road as an edge, you can treat the map as a weighted directed multigraph. An inter-city road is available if both its endpoint is recaptured.
Here comes the problem.
Given the traffic map, and the recaptured situation, can you tell Kudo what’s the shortest path from one city to another only passing the recaptured cities?

Input

The input consists of several test cases.
The first line of input in each test case contains three integers N (0<N≤300), M (0<M≤100000) and Q (0<Q≤100000), which represents the number of cities, the numbers of inter-city roads and the number of operations.
Each of the next M lines contains three integer x, y and z, represents there is an inter-city road starts from x, end up with y and the length is z. You can assume that 0<z≤10000.
Each of the next Q lines contains the operations with the following format:
a) 0 x – means city x has just been recaptured.
b) 1 x y – means asking the shortest path from x to y only passing the recaptured cities.
The last case is followed by a line containing three zeros.

Output

For each case, print the case number (1, 2 …) first.
For each operation 0, if city x is already recaptured (that is,the same 0 x operation appears again), print “City x is already recaptured.”
For each operation 1, if city x or y is not recaptured yet, print “City x or y is not available.” otherwise if Kudo can go from city x to city y only passing the recaptured cities, print the shortest path’s length; otherwise print “No such path.”
Your output format should imitate the sample output. Print a blank line after each test case.

Sample Input

3 3 6
0 1 1
1 2 1
0 2 3
1 0 2
0 0
0 2
1 0 2
1 2 0
0 2

0 0 0

Sample Output

Case 1:
City 0 or 2 is not available.
3
No such path.
City 2 is already recaptured.

Source

思路:马鸭这是什么鬼,我用floyd它t了,我用迪杰斯特拉它wa了,就这样郁闷了三个小时...后来发现它需要在每个 “0 x” 的情况,对点进行更新(用floyd的思路)就行,我T^T
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define eps 1e-9
#define pi acos(-1)

const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 1000 + 8;

bool sign[maxn], index[maxn];
int fig[maxn][maxn], n, m, q, u, v, w, num;

void init()
{
    for(int i = 0; i < maxn; i++)
        for(int j = 0; j < maxn; j++)
            if(i == j)fig[i][j] = 0;
            else
                fig[i][j] = inf;
}

void up(int k)
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(fig[i][j] > fig[i][k] + fig[k][j])
                fig[i][j] = fig[i][k] + fig[k][j];
}

int main()
{
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 0;
    while(cin >> n >> m >> q && (n + m + q))
    {
        init();
        fill(sign, sign + maxn, 1);///1为这个点没被捕获
        for(int i = 0; i < m; i++)
        {
            cin >> u >> v >> w;
            fig[u][v] = min(w, fig[u][v]);

        }
        cout << "Case " << ++t << ":" << '\n';
        for(int i = 0; i < q; i++)
        {
            cin >> num;
            if(num == 0)
            {
                cin >> u;
                if(!sign[u])
                    cout<< "City " << u << " is already recaptured." << '\n';
                else
                {
                    sign[u] = 0;///标记这个点被捕获了
                    up(u);
                }
            }
            else if(num == 1)
            {
                cin >> u >> v;
                if(sign[u] || sign[v])
                    cout << "City " << u << " or " << v << " is not available." << '\n';
                else
                {
                    if(fig[u][v] == inf)
                        cout << "No such path." << '\n';
                    else
                        cout << fig[u][v] << '\n';
                }
            }
        }
        cout << '\n';
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/11477654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值