hdu1599 flody求最小环 find the mincost route
2018-03-18 15:55
288 查看
find the mincost route
Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6376 Accepted Submission(s): 2521
[align=left]Problem Description[/align]杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。
[align=left]Input[/align]第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
[align=left]Output[/align]对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It's impossible.".
[align=left]Sample Input[/align]3 3
1 2 1
2 3 1
1 3 1
3 3
1 2 1
1 2 3
2 3 1
[align=left]Sample Output[/align]3
It's impossible.#include<cstdio>
#include<iostream>
using namespace std;
const int mn=105,inf=99999999;
int f[mn][mn],g[mn][mn],n,m;
int main() {
while(~scanf("%d%d",&n,&m)) {
int u,v,w;
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
f[i][j]=g[i][j]=inf;
while(m--) {
scanf("%d%d%d",&u,&v,&w);
if(f[u][v]>w) {
f[u][v]=f[v][u]=g[u][v]=g[v][u]=w;
}
}
int ans=inf;
for(int k=1; k<=n; ++k) {
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
if(i!=j&&i!=k&&j!=k) {
//没更新k则f[i][j]必定没有经过k点,这时把k点加进去
ans=min(ans,f[i][j]+g[j][k]+g[k][i]);
}
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
if(i!=j&&i!=k&&j!=k) f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}
if(ans==inf)
puts("It's impossible.");
else
printf("%d\n",ans);
}
return 0;
}
#include<cstdio> #include<iostream> using namespace std; const int mn=105,inf=99999999; int f[mn][mn],g[mn][mn],n,m; int main() { while(~scanf("%d%d",&n,&m)) { int u,v,w; for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) f[i][j]=g[i][j]=inf; while(m--) { scanf("%d%d%d",&u,&v,&w); if(f[u][v]>w) { f[u][v]=f[v][u]=g[u][v]=g[v][u]=w; } } int ans=inf; for(int k=1; k<=n; ++k) { for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) if(i!=j&&i!=k&&j!=k) { //没更新k则f[i][j]必定没有经过k点,这时把k点加进去 ans=min(ans,f[i][j]+g[j][k]+g[k][i]); f[i][j]=min(f[i][j],f[i][k]+f[k][j]); } } if(ans==inf) puts("It's impossible."); else printf("%d\n",ans); } return 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])
- find the mincost route HDU - 1599(带权最小环)(Floyd)
- hdu 1599 find the mincost route Floyd 无向图求最小环
- hdu1599 find the mincost route floyd求最小环
- hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)
- HDU 1599 find the mincost route (无向图floyd最小环详解)
- hdu 1599 find the mincost route(flyod求最小环)
- HDU1599 find the mincost route(floyd求最小环)
- HDU 1599 find the mincost route 无向图最小环
- hdu 1599 find the mincost route【floyd求最小环】
- poj 1734 Sightseeing trip hdu 1599 find the mincost route 最小环 解题报告
- HDU1599 find the mincost route 无向图最小环
- hdu 1599 find the mincost route 最小环
- HDU - 1599 find the mincost route(Floyd求最小环)
- HDU 1599 find the mincost route(无向图最小环)