hdu-1599-find the mincost route(弗洛伊德 最小环)
来源:互联网 发布:c语言表白源代码 编辑:程序博客网 时间:2024/05/16 08:06
find the mincost route
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5461 Accepted Submission(s): 2184
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)。
接下来的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.
根据题意是让求最小环,根据弗洛伊德算法进行改进,
C++ Code
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
#include<bits/stdc++.h>
#define INF 100000000
using namespace std;
int n,m;
int dist[101][101],mp[101][101],ans;
void floyd()
{
for(int k=1; k<=n; k++)
{
/// 一个环至少要 3个互不相同点,所以保证 k大于 i,i大于 j
for(int i=1; i<k; i++)
{
for(int j=i+1; j<k; j++)
{
ans=min(ans,mp[i][k]+mp[k][j]+dist[i][j]);
}
}
///把求最短路的循环放在求最小环的循环下边是为了保证dis[i][j]中没有k
///k点,造成重路.
/// ans = min ( ans , map[i][k] + map[k][j] + dist[i][j] ) 求最小环
///式子要求的就是 dist[i][j]中所有的中间点一定小于 k,所以不会重路
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1; i<=n; i++)
{
///对两个邻接表进行初始化
for(int j=1; j<=n; j++)
mp[i][j]=dist[i][j]=INF;
dist[i][i]=mp[i][i]=0;
}
for(int i=1; i<=m; i++)
{
///根据输入的值,对两个图进行更新
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
dist[x][y]=dist[y][x]=mp[x][y]=mp[y][x]=min(mp[x][y],z);
}
ans=INF;
floyd();
if(ans==INF)
{
printf("It's impossible.\n");
}
else
printf("%d\n",ans);
}
return 0;
}
#define INF 100000000
using namespace std;
int n,m;
int dist[101][101],mp[101][101],ans;
void floyd()
{
for(int k=1; k<=n; k++)
{
/// 一个环至少要 3个互不相同点,所以保证 k大于 i,i大于 j
for(int i=1; i<k; i++)
{
for(int j=i+1; j<k; j++)
{
ans=min(ans,mp[i][k]+mp[k][j]+dist[i][j]);
}
}
///把求最短路的循环放在求最小环的循环下边是为了保证dis[i][j]中没有k
///k点,造成重路.
/// ans = min ( ans , map[i][k] + map[k][j] + dist[i][j] ) 求最小环
///式子要求的就是 dist[i][j]中所有的中间点一定小于 k,所以不会重路
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1; i<=n; i++)
{
///对两个邻接表进行初始化
for(int j=1; j<=n; j++)
mp[i][j]=dist[i][j]=INF;
dist[i][i]=mp[i][i]=0;
}
for(int i=1; i<=m; i++)
{
///根据输入的值,对两个图进行更新
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
dist[x][y]=dist[y][x]=mp[x][y]=mp[y][x]=min(mp[x][y],z);
}
ans=INF;
floyd();
if(ans==INF)
{
printf("It's impossible.\n");
}
else
printf("%d\n",ans);
}
return 0;
}
阅读全文
1 0
- hdu-1599-find the mincost route(弗洛伊德 最小环)
- HDU 1599 find the mincost route(Floyd最小环)
- hdu 1599 find the mincost route(最小环)
- 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求最小环)
- 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
- 【HDU 1599】find the mincost route 【最小环】
- HDU 1599 find the mincost route 最小环
- HDOJ--1599--find the mincost route(floyd+最小环)
- hdu 1599 find the mincost route(找无向图最小环)
- HDU 1599 find the mincost route(无向图最小环)
- HDU 1599 find the mincost route (无向图floyd最小环详解)
- hdu 1599 find the mincost route(无向图最小环,floyd)
- 洛谷【P1404】加分二叉树
- 编译 libcurl 7.45.0 + zlib 1.2.8 + openssl 1.0.2d
- caffe:Model-Zoo
- lombok
- 1 深入理解计算机系统之计算机系统漫游
- hdu-1599-find the mincost route(弗洛伊德 最小环)
- HDU求奇数的乘积
- linux命令---GNU awk介绍
- widget背景图片自适应大小
- Qt之设置窗体居中显示
- AspectJ使用实例
- 51Nod-1623-完美消除
- 数据库的三大并发事务问题与四大隔离级别
- 我加入了互联网公司