题目链接

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.

分析:

Floyd 算法保证了最外层循环到 k 时所有顶点间已求得以 0…k-1 为中间点的最短路径。一个环至少有3个顶点,设某环编号最大的顶点为 L ,在环中直接与之相连的两个顶点编号分别为 M 和 N (M,N < L),则最大编号为 L的最小环长度即为 Graph(M,L) + Graph(N,L) + Dist(M,N) ,其中 Dist(M,N) 表示以 0…L-1 号顶点为中间点时的最短路径,刚好符合 Floyd 算法最外层循环到 k=L 时的情况,则此时对 M 和 N 循环所有编号小于 L的顶点组合即可找到最大编号为 L 的最小环。再经过最外层 k 的循环,即可找到整个图的最小环。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 110;
const int inf = 1000000;
int dist[maxn][maxn];
int e[maxn][maxn];
int n,m;
void initial()
{
int i;
int j;
for(i = 1 ; i <= n ; ++i)
{
for(j = 1 ; j <= n ; ++j)
{
if(i == j)
e[i][j] = 0;
else
e[i][j] = inf;
}
}
} int floyd()
{
int i;
int j;
int k; int mincircle = inf;
// dist = e;
for(i = 1 ; i <= n ; ++i)
{
for(j = 1 ; j <= n ; ++j)
{
dist[i][j] = e[i][j];
}
} //根据Floyed的原理,在最外层循环做了k-1次之后,dis[i][j]则代表了i到j的路径中所有结点编号都小于k的最短路径
for(k = 1 ; k <= n ; ++k)
{
//环的最小长度为edge[i][k]+edge[k][j]+i->j的路径中所有编号小于k的最短路径长度
for(i = 1 ; i < k ; ++i)
for(j = i+1 ; j < k ; ++j)
if(dist[i][j] + e[i][k] + e[k][j] < inf)
mincircle = min(mincircle,dist[i][j] + e[j][k] + e[k][i]);
//floyd原来的部分,更新dist[i][j]
for(i = 1 ; i <= n ; ++i)
for(j = 1 ; j <= n ; ++j)
if(dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j]; }
return mincircle;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
initial();
int i;
for(i = 1 ; i <= m ; ++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(e[a][b] > c)
e[a][b] = e[b][a] = c;
}
int ans = floyd();
if(ans != inf)
printf("%d\n",ans);
else
printf("It's impossible.\n");
}
return 0;
}

HDU 1599 find the mincost route (最短路 floyd)的更多相关文章

  1. HDU 1599 find the mincost route (无向图floyd最小环详解)

    转载请注明出处:http://blog.csdn.net/a1dark 分析:终于弄懂了floyd的原理.以前的理解一直肤浅.所以一做到floyd应用的题.就拙计了.其实floyd的本质DP.利用前K ...

  2. HDU 1599 find the mincost route(floyd求最小环 无向图)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  3. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  4. hdu 1599 find the mincost route(无向图的最小环)

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 1599 find the mincost route 最小环

    题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...

  6. hdu 1599 find the mincost route floyd求无向图最小环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. hdu 1599 find the mincost route

    http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...

  8. hdu 1599 find the mincost route(flyod求最小环)

    Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...

  9. HDU 1599 find the mincost route (无向图的最小环)

    题意: 给一个带权无向图,求其至少有3个点组成的环的最小权之和. 思路: (1)DFS可以做,实现了确实可以,只是TLE了.量少的时候应该还是可以水一下的.主要思路就是,深搜过程如果当前点搜到一个点访 ...

随机推荐

  1. Android api level对照表

    转自:blog.csdn.net/lihenair/article/details/49869299 Platform Version API Level VERSION_CODE Notes And ...

  2. C/S结构 B/S结构

    [1]C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应 ...

  3. 使用gdb查看栈帧的情况, 没有ebp

    0x7fffffffdb58: 0x004005ba  0x00000000  0x00000000  0x00000000 <-----funcb的栈帧 [0x7fffffffdb60, 0x ...

  4. Calendar简单用法

  5. BZOJ4710 JSOI2011分特产(容斥原理+组合数学)

    显然可以容斥去掉每人都不为空的限制.每种物品分配方式独立,各自算一个可重组合乘起来即可. #include<iostream> #include<cstdio> #includ ...

  6. P1483 序列变换

    题目描述 给定一个由n个整数构成的序列,你需要对它进行如下操作: 操作1:输入格式“1 x y”,表示把所有a[kx](k为正整数,kx<=n)都加上y. 操作2:输入格式“2 j”,表示输出a ...

  7. 923c C. Perfect Security

    Trie树. 要求字典序最小,所以由前到后贪心的选择.建一个trie树维护b数列. #include<cstdio> #include<algorithm> #include& ...

  8. BZOJ2223:[Coci2009]PATULJCI——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2223 Description Sample Input 10 3 1 2 1 2 1 2 3 2 3 ...

  9. Unity3D LOD Group

    今天下了一个4.0破解版,然后看到一个Demo Level of Detail    就研究了一下  以前用的是Unity3.5 free版本,没有这个功能,真实泪奔....... As your s ...

  10. EurekaServer集群配置

    一.程序配置 1.pom添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <ar ...