HDU 1599 find the mincost route (最短路 floyd)
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)的更多相关文章
- HDU 1599 find the mincost route (无向图floyd最小环详解)
转载请注明出处:http://blog.csdn.net/a1dark 分析:终于弄懂了floyd的原理.以前的理解一直肤浅.所以一做到floyd应用的题.就拙计了.其实floyd的本质DP.利用前K ...
- 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 ...
- 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 ...
- hdu 1599 find the mincost route(无向图的最小环)
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 1599 find the mincost route 最小环
题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...
- 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 ...
- hdu 1599 find the mincost route
http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...
- hdu 1599 find the mincost route(flyod求最小环)
Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...
- HDU 1599 find the mincost route (无向图的最小环)
题意: 给一个带权无向图,求其至少有3个点组成的环的最小权之和. 思路: (1)DFS可以做,实现了确实可以,只是TLE了.量少的时候应该还是可以水一下的.主要思路就是,深搜过程如果当前点搜到一个点访 ...
随机推荐
- java 基础--继承--007
1,子类只能继承父类所有非私有成员 2,子类不能继承父类的构造方法,但可以通过super去访问父类构造方法 3,子类成员变量和父类成员变量名称不一样,如果一样类似于重写,按子类处理,如果一样,就近原则 ...
- 在网页中浏览PDF文档
刚开始找了好多插件,包括pdf.js,但都不理想,后来发现用iframe反而容易: <iframe src="test_pdf.pdf" width="800&qu ...
- 【hdu3555】Bomb 数位dp
题目描述 求 1~N 内包含数位串 “49” 的数的个数. 输入 The first line of input consists of an integer T (1 <= T <= 1 ...
- 【bzoj1616】[Usaco2008 Mar]Cow Travelling游荡的奶牛 bfs
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- XML-RPC协议学习
XML-RPC调用包括2部分:客户端client(调用线程).服务器端server(被调用的线程).服务端是通过特定的URL获得的,调用过程如下: 1.客户端程序使用XML-RPC客户端发出作业请求, ...
- CentOS 访问控制列表(tcp wrappers)
1.TCP Wrappers是一个工作在应用层的安全工具,它只能针对某些具体的应用或者服务起到一定的防护作用.比如说ssh.telnet.FTP等服务的请求,都会先受到TCP Wrappers的拦截. ...
- POJ3090:Visible Lattice Points——题解
http://poj.org/problem?id=3090 题目大意:你站在(0,0)的点上看向第一向限的点,点和点会互相阻挡,问最多看到多少点. 很容易想到,我们能看到的点,它的横纵坐标一定是互质 ...
- BZOJ4873:[SHOI2017]寿司餐厅——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4873 https://www.luogu.org/problemnew/show/P3749 简要题 ...
- ZOJ3496:Assignment——题解
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3496 题目大意:A公司从S到T运货,每条路都有一个运货上限,而B公司则有p ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...