UESTCoj 265 War(Floyd最短路)
2015-09-01 17:56
507 查看
题目描述: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.
Floyd算法求最短路的原理是枚举每个点做为中点,更新起点和终点的最短距离。
按照题意中的意思是要拆除一些点后再求最短路,我们可以倒着去做认为是新建一些点后再去求最短路,对于每个新建点k,我们只要以k为中点更新一下最短路就行了,更新的过程是O(n^2)的。然后最多新建n个点,所以整个算法复杂度为O(n^3)。
PS:我觉得暴力SPFA应该可以过的,然后就T了。
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.
Floyd算法求最短路的原理是枚举每个点做为中点,更新起点和终点的最短距离。
按照题意中的意思是要拆除一些点后再求最短路,我们可以倒着去做认为是新建一些点后再去求最短路,对于每个新建点k,我们只要以k为中点更新一下最短路就行了,更新的过程是O(n^2)的。然后最多新建n个点,所以整个算法复杂度为O(n^3)。
PS:我觉得暴力SPFA应该可以过的,然后就T了。
#pragma warning(disable:4996) #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; int dis[205][205]; bool exist[205]; vector<int>ans; int query[10005], x[10005], y[10005]; int n, m; int main(){ int t; scanf("%d", &t); while (t--){ scanf("%d %d", &n, &m); memset(dis, 0x3f, sizeof dis); memset(exist, true, sizeof exist); for (int i = 1; i <= n; i++)dis[i][i] = 0; for (int i = 0; i < m; i++){ int u, v, c; scanf("%d %d %d", &u, &v, &c); dis[u][v] = dis[v][u] = min(dis[u][v], c); } int q; scanf("%d", &q); for (int i = 1; i <= q; i++){ int op; scanf("%d", &op); if (op == 0){ int u, v; scanf("%d %d", &u, &v); query[i] = 0; x[i] = u; y[i] = v; } else if (op == 1){ int u; scanf("%d", &u); exist[u] = false; query[i] = 1; x[i] = u; } } //Floyd求出最短路 for (int k = 1; k <= n; k++){ if (!exist[k])continue; for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } } ans.clear(); for (int i = q; i >= 1; i--){ if (query[i] == 0){ if (exist[x[i]] && exist[y[i]]){ int tmp = dis[x[i]][y[i]]; tmp = tmp == dis[0][0] ? -1 : tmp; ans.push_back(tmp); } else ans.push_back(-1); } //加点 else if (query[i] == 1){ int k = x[i]; exist[k] = true; for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } } } for (int i = (int)ans.size() - 1; i >= 0; i--){ printf("%d\n", ans[i]); } puts(""); } return 0; }
相关文章推荐
- UESTC 360 Another LCIS(线段树 x 经典区间合并姿势)
- 58.UITableView:改变 TableHeaderView 的高度--跟随cell滚动
- IOS-笔记20(Embed Segues)
- UISlider控件基本用法
- 对TimeQuest一些术语的解释
- UE3 Creating A Mesh Proxy
- Error之ViewPage :Required method instantiateItem was not overridde
- 36.在iPhone4S上,UIButton-setEnabled属性为NO时,无法改变button的状态,如上面的文字
- IOS8如何获取当前UIViewController
- 28.iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- source /build/envsetup.sh和lunch)
- 19.UIDatePicker日期选择器
- iOS开发之指定UIView的某几个角为圆角
- UIActivityViewController 自定义选项
- Echarts异步模块定义(AMD规范)——require.js
- UIImage加载图片方式一般有两种
- UITextField中加入清除按钮
- 9..IOS学习:用UIWindow自定义AlertView(最基本代码)
- HDU 2227 Find the nondecreasing subsequences(DP)
- 7.IOS 8 UIUserNotificationSettings 推送