HDU 5137 How Many Maos Does the Guanxi Worth(弗罗伊德)
来源:互联网 发布:银行家算法安全性算法 编辑:程序博客网 时间:2024/05/08 19:06
题目:How Many Maos Does the Guanxi Worth
题意:给定N个点和M条边,点编号是1到N。现在要从2到N-1中选择一个删除,同时跟选择的点连接的边也就消失,然后使得点1到N的最短路径的长度最大。如果点1和点N不连通,则输出“Inf"。
按照题意,N最多30,而M可以达到1000,应该是存在重边的。
可以设定邻接矩阵f,不存在的边设为inf,存在的边保留最小值。
然后枚举要删除的点,假设删除x,把f数组的内容复制到dp过去,然后dp中跟x有关的边全部设置为inf。
对dp跑一遍弗罗伊德算法,dp[1][N]就是删除x的情况下,1到N的最短路径,所有最短路径取最大值即可。
弗罗伊德的复杂度是O(N^3),加上x的枚举是O(N^4),不过N才30,够了。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 40;const int inf = 1000000000;int n, m, f[N][N], dp[N][N];int solve(int x){for(int i=1; i<=n; i++){for(int j=1; j<=n; j++){if(i==x || j==x)dp[i][j] = inf;elsedp[i][j] = f[i][j];}}for(int k=1; k<=n; k++){for(int i=1; i<=n; i++){for(int j=1; j<=n; j++)dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);}}return dp[1][n];}int main(){while(~scanf("%d %d", &n, &m) && (n||m)){for(int i=1; i<=n; i++){for(int j=1; j<=n; j++)f[i][j] = i==j?0:inf;}int a, b, c;while(m--){scanf("%d %d %d", &a, &b, &c);f[a][b] = min(f[a][b], c);f[b][a] = f[a][b];}int ans = 0;for(int i=2; i<n; i++){ans = max(ans, solve(i));}if(ans==inf)puts("Inf");elseprintf("%d\n", ans);}return 0;}
0 0
- HDU 5137 How Many Maos Does the Guanxi Worth(弗罗伊德)
- Hdu oj 5137 How Many Maos Does the Guanxi Worth
- HDU 5137:How Many Maos Does the Guanxi Worth【Floyd】
- HDU - 5137 How Many Maos Does the Guanxi Worth
- HDU 5137 How Many Maos Does the Guanxi Worth
- HDU 5137 How Many Maos Does the Guanxi Worth
- HDU-5137 H - How Many Maos Does the Guanxi Worth
- HDU 5137How Many Maos Does the Guanxi Worth
- hdu 5137 ( How Many Maos Does the Guanxi Worth )
- How Many Maos Does the Guanxi Worth
- How Many Maos Does the Guanxi Worth
- How Many Maos Does the Guanxi Worth
- How Many Maos Does the Guanxi Worth
- How Many Maos Does the Guanxi Worth
- How Many Maos Does the Guanxi Worth
- hdu 5137 How Many Maos Does the Guanxi Worth(最短路径)
- HDU 5137 How Many Maos Does the Guanxi Worth(最短路径)
- HDU 5137 How Many Maos Does the Guanxi Worth(floyd记录路径)
- vim多窗口编辑
- flex计算器
- Object-C加强 Protocol协议
- 进程间通信(IPC)
- linux goahead2.1.8移植
- HDU 5137 How Many Maos Does the Guanxi Worth(弗罗伊德)
- UVA - 10986 Sending email 最短路的spfa算法
- 文件的打开并输出
- 让照片导入苹果iphone手机相机胶卷,不用在电脑删除,直接在手机上删除照片图文教程
- Javadoc 执行后报告java.lang.NullPointerException问题求解
- 好文转载Apache Maven 入门篇(下)
- QTP自动化测试
- "><img src=1 onerror=alert(document.cookie)>
- LeetCode OJ 之 Intersection of Two Linked Lists (两个链表的交点)