find the mincost route

Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3425    Accepted Submission(s): 1397

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.
 
Author
8600
题解:
错到无奈。。。其实这道题让球至少经过三个城市的最短环,用的思想就是用map记录起初的各边权值,然后用dis记录最短路,然后
anser=MIN(anser,dis[i][j]+map[j][k]+map[k][i]);保证了最小经过三个城市的最小环的权值和;dis【i】[j]代表从i到j城市的最短路
代码:
 #include<stdio.h>
#include<string.h>
#define MIN(x,y)(x<y?x:y)
const int INF=0x3f3f3f;
const int MAXN=;
int map[MAXN][MAXN],dis[MAXN][MAXN],anser;
int N,M;
void initial(){
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(i-j)map[i][j]=INF;
else map[i][j]=;
}
void floyd(){
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
dis[i][j]=map[i][j];
anser=INF;
for(int k=;k<=N;k++){
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
if(i!=j&&i!=k&&j!=k)
anser=MIN(anser,dis[i][j]+map[j][k]+map[k][i]);
/*if(anser>dis[i][j]+map[j][k]+map[k][i]){
printf("dis[%d][%d]=%d\n",i,j,dis[i][j]);
printf("map[%d][%d]=%d\n",j,k,map[j][k]);
printf("map[%d][%d]=%d\n",k,i,map[k][i]);
}*/
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
dis[i][j]=MIN(dis[i][j],dis[i][k]+dis[k][j]);//弄成maple错了半天。。。。
}
if(anser==INF)puts("It's impossible.");
else printf("%d\n",anser);
}
int main(){
int a,b,c;
while(~scanf("%d%d",&N,&M)){
initial();
while(M--){
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])map[a][b]=map[b][a]=c;
}
floyd();
}
return ;
}

find the mincost route(最小环,最短路,floyd)的更多相关文章

  1. hdu 1599 find the mincost route 最小环

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

  2. hdoj 1599 find the mincost route【floyd+最小环】

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

  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. HD1599 find the mincost route(floyd + 最小环)

    题目链接 题意:求最小环 第一反应时floyd判断,但是涉及到最少3个点,然后就不会了,又想的是 双联通分量,这个不知道为什么不对. Floyd 判断 最小环 #include <iostrea ...

  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. hdu1599 find the mincost route floyd求出最小权值的环

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

  8. [hdu P1599] find the mincost route

    [hdu P1599] find the mincost route 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V ...

  9. 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 ...

随机推荐

  1. 管道通信之无名管道---pipe()

    pipe()函数在子进程产生之前就应该存在. 父子进程之间只进行一次传递 /*============================================ > Copyright ( ...

  2. 0-20ma 0-5V,0-10V ,0-15V ,0-20V,0-30V模拟量(范围可以定制)多功能采集模块,支持1路继电器输出,2路Di输入,8路Ai输入,可电脑控制,支持485 modbus rtu协议。端口参数可以配置保存,支持定制修改。

    多功能模拟量采集模块MRD-5017具有8 通道模拟量采集(支持0-20mA,0-5V,0-10V混合测量),2路DI,1路继电器输出,1路485接口(支持MODBUS RTU),能实现8路AI(12 ...

  3. jQuery学习-事件之绑定事件(四)

    今天我们来学习jQuery.Event对象.jQuery为了添加自己的处理机制,及可以传递用户自定义数据,于是Event对象就出世了.   1 jQuery.Event = function( src ...

  4. 使用正则表达式限制TextBox输入

    /// <summary> /// 使用正则表达式限制输入 /// </summary> public class TextBoxRegex : TextBox { // 正则 ...

  5. perl 对象 bless 引用

    [root@dr-mysql01 ~]# cat aa.pl use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new ...

  6. java中读取程序运行时间

    第一种是以毫秒为单位计算的. Java代码 //伪代码 long startTime=System.currentTimeMillis();   //获取开始时间 doSomeThing();  // ...

  7. MAC COCOA call command 调用终端控制台程序

    MAC COCOA call command 调用终端控制台程序 STEP 1 先写一个C++ DOS程序 STEP2 使用NSTask来运行,然后用NSPipe和 NSData来接受运行的结果字符串 ...

  8. jQuery+PHP实现的砸金蛋中奖程序

    准备 我们需要准备道具(素材),即相关图片,包括金蛋图片.蛋砸碎后的图片.砸碎后的碎花图片.以及锤子图片. HTML 我们页面上要展现的是一个砸金蛋的台子,台上放了编号为1,2,3的三个金蛋,以及一把 ...

  9. Quartz.NET 2.0 作业调度框架使用

    Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不 ...

  10. PHP自学3——在html的<table>标签中显示用户提交表单

    为了更好地显示用户提交表单,本节将在上一节的基础上将读取的用户表单显示在html的<table>标签中,这一节将用到和数组有关的知识. 本节代码将从外部文件(.txt文件)中读取信息于指定 ...