HDU 1599 find the mincost route
来源:互联网 发布:excel怎么建立数据库 编辑:程序博客网 时间:2024/05/17 19:16
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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)。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
Output
对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It's impossible.".
Sample Input
3 31 2 12 3 11 3 13 31 2 11 2 32 3 1
Sample Output
3It's impossible.
找出从一个景点出发到其他点再返回的最短路径,相当于一个环,环中最少有两个点,假如k为环中最大的点,i,j为与k相邻的点,则lin[i][k]+lin[j][k]+map[i][j] 为环的长度,其中min[i][k]代表i到k(i与k相邻,所以等价 i 直接到k的距离)的距离,map[i][j]表示i到j的最短距离(可能经过其他的点),ans = min(ans,lin[i][k] + lin[j][k] + map[i][j]); ans表示图中所有可能成的环的长度,求出最小值。
#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0xfffffff using namespace std; int map[1010][1010],dis[1010][1010]; int n,m,minn; void spfa() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<k;i++) { for(j=i+1;j<k;j++) { minn=min(minn,map[i][k] + map[k][j] + dis[i][j]); //表示环的长度 } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); //更新i到j经过k点的最短距离 } } } } int main() { int i,j,a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { map[i][j] = dis[i][j] = ((i == j)?0:INF); } } for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); if(c < map[a][b]) { map[a][b] = map[b][a] = c;//表示a到b的最短距离 dis[a][b] = dis[b][a] = c;//表示a直接到b的距离 } } minn=INF; spfa(); if(minn>=INF) printf("It's impossible.\n"); else printf("%d\n",minn); } 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
- jq编写轮播插件
- 多线程(一)
- 数据结构上机测试2-2:单链表操作B
- Android 实现点击两次退出
- C. Terse princess
- HDU 1599 find the mincost route
- Caught exception while loading file com/crud/action/LoginAction-validation.xml
- Orcale 视频知识点总结
- 【HDU 1599 find the mincost route 】
- <LeetCode OJ> 387. First Unique Character in a String
- Graphviz样例之有向图
- 面试总结(static、const、define)
- Freeline - Android平台上的秒级编译方案
- Android5.0以上版本ActiveAndroid报ClassNotFoundException错误