山东省第一届ACM大学生程序设计竞赛--Emergency--变形Floyd算法
来源:互联网 发布:mysql数据库查询工具 编辑:程序博客网 时间:2024/05/21 04:22
Emergency
Time Limit: 1000MS Memory limit: 65536K
题目描述
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?
输入
The input consists of several test cases.
The first line of input in each test case contains three integersN (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 fromx, end up withy and the length isz. 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.
输出
For each operation 0, if cityx is already recaptured (that is,the same 0 x operation appears again), print “Cityx is already recaptured.”
For each operation 1, if city x or y is not recaptured yet, print “Cityx 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.
示例输入
3 3 60 1 11 2 10 2 31 0 20 00 21 0 21 2 00 20 0 0
示例输出
Case 1:City 0 or 2 is not available.3No such path.City 2 is already recaptured.
提示
来源
示例程序#include <stdio.h>#include <string.h>#define maxint 99999999int main(){ int n , m, q , i , j , k = 1 ; int a[303][303] , b[303][303] ; int flag[303] ; while(scanf("%d %d %d", &n, &m, &q)!=EOF) { if(n == 0 && m == 0 && q == 0) break; memset(flag,0,sizeof(flag)); for(i = 0 ; i < n ; i++) for(j = 0 ; j < n ; j++) { if(i == j ) a[i][j] = 0 ; else a[i][j] = maxint ; b[i][j] = a[i][j] ; } int u ,v , w ; for(i = 0 ; i < m ; i++) { scanf("%d %d %d", &u, &v, &w); if(w < a[u][v]) a[u][v]= w ; } int s , x , y ; printf("Case %d:\n", k++); while(q--) { scanf("%d", &s); if(s) { scanf("%d %d", &x, &y); if(!flag[x] ||!flag[y]) printf("City %d or %d is not available.\n", x , y); else if(b[x][y] < maxint) printf("%d\n",b[x][y]); else printf("No such path.\n"); } else { scanf("%d", &x); if(flag[x]) { printf("City %d is already recaptured.\n", x); continue ; } flag[x] = 1 ; for(i = 0 ; i < n ; i++) { if(flag[i] == 0) continue ; if(a[x][i] < b[x][i]) b[x][i] = a[x][i] ; for(j = 0 ; j < n ; j++) { if(flag[j] == 0) continue ; if( b[x][j] > b[x][i] + b[i][j] ) b[x][j] = b[x][i] + b[i][j] ; } } for(i = 0 ; i < n ; i++) { if(flag[i] == 0) continue ; if(a[i][x] < b[i][x]) b[i][x] = a[i][x] ; for(j = 0 ; j < n ; j++) { if(flag[j] == 0) continue ; if(b[j][x] > b[j][i] + b[i][x] ) b[j][x] = b[j][i] + b[i][x] ; } } for(i = 0 ; i < n ; i++) for(j = 0 ; j < n ; j ++) if(b[i][j] > b[i][x] + b[x][j]) b[i][j] = b[i][x] + b[x][j] ; } } printf("\n"); } return 0;}
- 山东省第一届ACM大学生程序设计竞赛 Emergency floyd变形
- 山东省第一届ACM大学生程序设计竞赛--Emergency--变形Floyd算法
- 2010年山东省第一届ACM大学生程序设计竞赛——Emergency
- 山东省第一届ACM大学生程序设计竞赛--Greatest Number--二分
- 山东省第一届ACM大学生程序设计竞赛 Shopping 水。。。。。。。
- 山东省第一届ACM大学生程序设计竞赛 Balloons bfs搜索
- 山东省第一届ACM大学生程序设计竞赛-Balloons(搜索)
- 2010年山东省第一届ACM大学生程序设计竞赛:shopping
- 2010年山东省第一届ACM大学生程序设计竞赛:Balloons
- 山东省第一届ACM大学生程序设计竞赛 problemG Shopping
- 山东省第七届ACM大学生程序设计竞赛
- 第四届 山东省ACM大学生程序设计竞赛
- 第七届 山东省ACM大学生程序设计竞赛
- 2010年山东省第一届ACM大学生程序设计竞赛——Balloons
- Phone Number 2010年山东省第一届ACM大学生程序设计竞赛
- Hello World! 2010年山东省第一届ACM大学生程序设计竞赛
- 组队周赛_山东省第一届ACM大学生程序设计竞赛
- [2010山东省第一届ACM大学生程序设计竞赛]——Phone Number
- 输入5个学生的姓名和成绩,顺序输出这五个学生的姓名和成绩,并输出最高成绩的姓名和成绩
- 设计模式之代理
- Import/Export(EXP-00091)
- WebView本地java方法和js之间的调用
- PAT 1013. 数素数 (20)
- 山东省第一届ACM大学生程序设计竞赛--Emergency--变形Floyd算法
- ubuntu Android开发问题汇总(不定时更新)
- C++primer plus第六版课后编程题答案10.1
- 百度文本编辑器的问题
- 你所不知道的五件事情--java.util.concurrent(第一部分)
- 各种乱七八糟常用函数的简单用法(不定期更新)
- codeforces problem/416/A 二分
- 两种特殊的排序组合计数公式
- 一个人,一座城