find the mincost route(城市最小环)
来源:互联网 发布:南京市行知实验中学 编辑:程序博客网 时间:2024/06/07 23:56
find the mincost route(城市最小环)
杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
3 31 2 12 3 11 3 13 31 2 11 2 32 3 1
3It's impossible.
C++ Code1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 #include<iostream> #include<cstdio> const int N=111; const int inf=1000000; #define min(x,y) ((x<y)?(x):(y)); using namespace std; int dist[N][N]; int edge[N][N]; int n,m; void floyd() { int ans=inf; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { dist[i][j]=edge[i][j]; } } //根据Floyed的原理,在最外层循环做了k-1次之后,dis[i][j]则代表了i到j的路径中所有结点编号都小于k的最短路径 for(int k=1;k<=n;k++) {//环的最小长度为edge[i][k]+edge[k][j]+i->j的路径中所有编号小于k的最短路径长度 for(int i=1;i<k;i++) { for(int j=i+1;j<k;j++) { if(dist[i][j]+edge[i][k]+edge[k][j]<inf) { ans=min(ans,dist[i][j]+edge[i][k]+edge[k][j]); } } } //floyd原来的部分,更新dist[i][j]/// for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(dist[i][j]>dist[i][k]+dist[k][j]) { dist[i][j]=dist[i][k]+dist[k][j]; } } } } if(ans==inf) { printf("It's impossible.\n"); } else printf("%d\n",ans); } int main(){ while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) { edge[i][j]=0; } else edge[i][j]=inf; } } int x,y,cost; for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&cost); //考虑重边 if(edge[x][y]>cost) { edge[x][y]=edge[y][x]=cost; } } floyd(); } return 0; }
阅读全文
0 0
- find the mincost route(城市最小环)
- find the mincost route(城市最小环)
- HDU 1599 find the mincost route(Floyd最小环)
- HDOJ--1599--find the mincost route(floyd+最小环)
- hdu 1599 find the mincost route(最小环)
- HDU1599 find the mincost route (floyd求最小环)
- hdu1599—find the mincost route(floyed最小环)
- hdu-1599-find the mincost route(弗洛伊德 最小环)
- HDU1599 find the mincost route 无向图最小环
- HDU 1599 find the mincost route Floyd求最小环
- hdu1599 find the mincost route floyd求最小环
- HDU1599 find the mincost route(floyd求最小环)
- hdu 1599 find the mincost route floyd求最小环
- HDU 1599 find the mincost route (Floyd 最小环)
- HDU - 1599 find the mincost route(Floyd求最小环)
- hdu 1599 find the mincost route(floyd 最小环)
- hdoj 1599 find the mincost route 【floyd + 最小环】
- HDU 1599 find the mincost route(Floyd判最小环)
- 逃逸分析
- JAVA设计模式初探之装饰者模式
- CCNA第七天(帧中继协议)
- 容器/IO/线程知识补漏
- 报表FineReport部署Tomcat服务器
- find the mincost route(城市最小环)
- 表格与表单
- C++中vector的使用
- 深入 kernel panic 流程
- c++顶级社区
- 第18节-程序编辑器vim
- python基础五_列表生成式,生成器
- Spire.Doc 教程:将word文档中的隐藏文本保存到PDF
- mysql当前日期函数应用