HDU 1599[find the mincost route]题解
来源:互联网 发布:水中刀 知乎 编辑:程序博客网 时间:2024/05/22 12:00
(传送门)
题目大意
给出一个无向图,求这个图的最小环(环上至少有两条边,每个点只能经过一次)。
题目分析
对于一个无向图,并不是像有向图那样先floyd扫然后查找f[i][i]最小值,但仍然可以用floyd来解决。
在枚举最外层k的时候,此时所有的最短路都没有k,那么可以找出含k的最小环。
先说明,在某个环上的最大点编号为最外层枚举的k,现在有两个点i,j与k直接相连,且,那么最大点编号为k,i,j,k都在环上所对应的最小环的权值和为
所以可以先枚举k,然后枚举i < j < k,更新答案,接着再正常的用floyd算含路径中有k时的最短路。
时间:O(n^3); 空间:O(n^2);
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,e,ans,INF,cst[105][105],f[105][105];inline void readi(int &x){ x=0; char ch=getchar(); while ('0'>ch||ch>'9') ch=getchar(); while ('0'<=ch&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}}void floyd(){ for (int k=1;k<=n;k++) { for (int i=1;i<k;i++) for (int j=i+1;j<k;j++) if (f[i][j]!=INF&&cst[i][k]!=INF&&cst[j][k]!=INF) ans=min(ans,f[i][j]+cst[i][k]+cst[j][k]); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (f[i][k]!=INF&&f[k][j]!=INF) f[i][j]=min(f[i][j],f[i][k]+f[k][j]); } if (ans>=INF) printf("It's impossible.\n"); else printf("%d\n",ans);}int main(){ while (scanf("%d%d",&n,&e)==2) { memset(f,63,sizeof(f)); memset(cst,63,sizeof(cst)); INF=f[0][0]; for (int i=1;i<=n;i++) f[i][i]=cst[i][i]=0; for (int i=1;i<=e;i++) { int x,y,z; readi(x); readi(y); readi(z); if (z>=cst[x][y]) continue; cst[x][y]=cst[y][x]=f[x][y]=f[y][x]=z; } ans=INF; floyd(); } return 0;}
阅读全文
0 0
- HDU 1599[find the mincost route]题解
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- 【HDU 1599 find the mincost route】
- HDU 1599 find the mincost route
- 【HDU 1599 find the mincost route 】
- HDU 1599 find the mincost route
- 优先队列优化Dijkstra-hdu2066
- 51nod 1163 最高的奖励
- Android 实现通过url加载PDF
- 1113. Integer Set Partition (25)
- 【理论实践】实例体会一下using声明会屏蔽外层作用域的同名实体
- HDU 1599[find the mincost route]题解
- 2017 java面试题总览
- Systemstate Dump分析经典案例(下)
- 基于OnPaint的Slider进度条自绘实现
- windows命令行下的常见命令
- C++:标准模板库map
- 最牛语义匹配算法_速围观!
- 【HDU-1722】 Cake
- (并发)线程的创建