UESTC Training for Graph Theory——I、War

War

Time Limit: 4000 ms Memory Limit: 65536 kB Solved: 57 Tried: 690

Description

Your country is now involved in a war! In the front, there are N positions between which you can transfer goods. Unfortunately the enemies will attack you and destroy some positions. When transferring, you cannot pass through a position which has been destroyed. As the commander of the army, you want to know the minimum distance between some position u and v, after some positions have been destroyed.

Input

The first line of inputs gives t (t <= 15) indicating the number of test cases.
Then t blocks follow. The first line of each block gives n and m (2 <= n <= 200, 0 <= m <= 100000) indicating the number of positions and number of roads. Each of the next m lines contains 3 integers u, v and e (1 <= u, v <= n, 1 <= e <= 1000), telling that there is a road between position u and position v and the distance of the road is e. An integer q (q<=10000) follows in the next line representing the number of queries to process. Each of the next q lines has one of the following formats:
A. 0 u v: This means you should reply with the shortest path between position u and position v.
B. 1 u: This means position u is destroyed.

You should notice that all the positions are not destroyed initially and there could be more than one road between two positions.

Output

For all the queries in the format A:0 u v, if any of the queried position is destroyed or there is no path from u to v, print -1; otherwise print the shortest path between the two positions. Print a blank line after each test case.

Sample Input

1
4 4
1 2 1
1 3 3
2 4 2
3 4 4
5
0 1 4
1 2
0 1 4
1 3
0 1 4

Sample Output

3
7
-1

Source

经典问题

 

/*算法思想:
  给一个带权图,然后有一些操作:
  1、删掉每个点
  2、询问两点之间的最短路
  变相的floyd,灵活运用了floyd的思想,先把所有的操作读入,倒着处理,先把要删掉的点全部删掉
  然后用还存在在图中的点跑一遍floyd,倒着处理每个操作,要是这个操作是询问两点之间的最短路,
  直接给出答案,要是操作是删掉某个点,就把这个点加进来,先更新其他点到这个点的最短路,然后
  再用这个点去更新所有点对之间的最短路。
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 123456789
using namespace std;
struct data
{
    int bj;
    int v1,v2,ans;
} query[20000];
int n,m;
int g[300][300];
bool out[300];
int main()
{
    int t,a,b,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j) g[i][j]=INF;
                else g[i][j]=0;
        for(int  i=1;i<=m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(g[a][b]>c) g[a][b]=g[b][a]=c;
        }
        int Q;
        scanf("%d",&Q);
        memset(out,0,sizeof(out));
        for(int i=1;i<=Q;i++)
        {
            scanf("%d",&query[i].bj);
            if(query[i].bj)
            {
                scanf("%d",&query[i].v1);
                out[query[i].v1]=true;
            }
            else scanf("%d%d",&query[i].v1,&query[i].v2);
        }
        for(int k=1;k<=n;k++)  //把所有的点删掉之后用还在图中的点跑一遍floyd
        if(!out[k])
            for(int i=1;i<=n;i++)
            if(!out[i])
                for(int j=1;j<=n;j++)
                if(!out[j])
                    g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
        for(int i=Q;i>=1;i--)
        {
            if(query[i].bj)
            {
                int k=query[i].v1;
                out[k]=false;
                for(int i=1;i<=n;i++)  //先更新其他点到这个点的最短路
                if(!out[i])
                    for(int j=1;j<=n;j++)
                    if(!out[j])
                        g[i][k]=min(g[i][k],g[i][j]+g[j][k]);
                for(int i=1;i<=n;i++)
                    if(!out[i]) g[k][i]=g[i][k];
                for(int i=1;i<=n;i++)  //再用这个点更新所有点对之间的最短路
                if(!out[i])
                    for(int j=1;j<=n;j++)
                    if(!out[j])
                        if(g[i][j]>g[i][k]+g[k][j]) g[i][j]=g[i][k]+g[k][j];
            }
            else
            {
                if(!out[query[i].v1] && !out[query[i].v2] && g[query[i].v1][query[i].v2]<INF)
                    query[i].ans=g[query[i].v1][query[i].v2];  //存储当前的最短路
                else query[i].ans=-1;  //不能到达
            }
        }
        for(int i=1;i<=Q;i++)
            if(!query[i].bj)
                printf("%d\n",query[i].ans);
        printf("\n");
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值