hdu 1599 find the mincost route
来源:互联网 发布:淘宝客 嗨推 编辑:程序博客网 时间:2024/06/03 12:29
Problem Description
杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,….VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。
Input
第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
Output
对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出”It’s impossible.”.
Sample Input
3 3
1 2 1
2 3 1
1 3 1
3 3
1 2 1
1 2 3
2 3 1
Sample Output
3
It’s impossible.
设某环的最大顶点为k,环中与k直接连接的点为i,j,则最小环长度为map[i][k]+map[k][j]+dis[i][j]。另外此题的无穷不宜设置得太大,否则会发生溢出。
#include<iostream>#include<cstring>#include<cstdio>using namespace std;#define inf 10000int dis[200][200],map[200][200];long long minn,n;void floyd(){ int k,i,j;minn=inf; for(k=1;k<=n;k++) { for(i=1;i<k;i++) for(j=i+1;j<k;j++) { if(minn>map[i][k]+map[k][j]+dis[i][j]) minn=map[i][k]+map[k][j]+dis[i][j]; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(dis[i][j]>dis[i][k]+dis[k][j]) dis[i][j]=dis[i][k]+dis[k][j]; } } if(minn>=inf) cout<<"It's impossible."<<endl; else cout<<minn<<endl;} int main(){ int m,i,j; while(cin>>n>>m) { for( i=0;i<=n;i++) for( j=0;j<=n;j++) { if(i==j) map[i][j]=dis[i][j]=0; else map[i][j]=dis[i][j]=inf; } for(i=0;i<m;i++) { int st,en,len; cin>>st>>en>>len; if(len<map[st][en]) map[st][en]=map[en][st]=dis[en][st]=dis[st][en]=len; } 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
- 对自由变量的理解
- Android视图绘制流程完全解析,带你一步步深入了解View(二)
- 使用 udev 高效、动态地管理 Linux 设备文件
- iOS7以后 视图Frame坐标起点发生的变化
- Scala中List的ListBuffer实现高效的遍历计算
- hdu 1599 find the mincost route
- 黑马程序员——Java基础——Object,包和多线程基础(一)
- Android手势监听类GestureDetector
- tomcat源码系列(一)--组合项目导入ide
- solrUtil工具类
- 【Linux】——实用命令
- ffmpeg函数调用关系图
- 关于 iptables 和 tc 的限速理解
- 回溯法概述—BackTracking