UESTC Training for Graph Theory——I、War
来源:互联网 发布:命令行编译java文件 编辑:程序博客网 时间:2024/05/11 03:35
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 123456789using 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;}
- UESTC Training for Graph Theory——I、War
- 2014 UESTC Training for Graph Theory I
- UESTC Training for Graph Theory——A、Railway
- UESTC Training for Graph Theory——B、Asteroids
- UESTC Training for Graph Theory——c、Catenyms
- UESTC Training for Graph Theory——D、Distance Queries
- UESTC Training for Graph Theory——E、Distance Queries
- UESTC Training for Graph Theory——F、Instantaneous Transference
- UESTC Training for Graph Theory——G、Labeling Balls
- UESTC Training for Graph Theory——H、Redundant Paths
- UESTC Training for Graph Theory——K、Blinking Stalkers
- UESTC Training for Graph Theory——L、House Man
- UESTC Training for Graph Theory——M、Network
- 2014 UESTC Training for Graph Theory B
- 2014 UESTC Training for Graph Theory C
- 2014 UESTC Training for Graph Theory F
- 2014 UESTC Training for Graph Theory G
- 2014 UESTC Training for Graph Theory H
- J2SE的知识点之(监听适配器)
- UESTC Training for Graph Theory——H、Redundant Paths
- Struts2.2基本配置
- 使用Spring3.1.1的缓存示例
- Android 使用cmwap访问互联网的办法
- UESTC Training for Graph Theory——I、War
- 六种方式实现hibernate查询,及IDE推荐
- 黑马程序员_学习日记23_C#基础知识补充(StringBuilder、ArrayList、HashTable)
- c语言数据输入输出
- UESTC Training for Graph Theory——J、Going back home
- 过了30岁,程序员该怎么办?
- Android 多线程编程
- 使用MFC插入Excel工作表并实现自动化
- 黑马程序员_学习日记24_面向对象技术高级应用(接口、抽象类与抽象方法、密封类与密封方法)