//代码:

//方法1:Dijkstra's Algorithm

#include<stdio.h>
#include<math.h>
#include<string.h>
#define INF 0xfffffff
#define N 110
#define min(a, b)(a < b ? a : b) int maps[N][N];
int d[N], visit[N];
int n, m; void Init()
{
int i, j;
memset(visit, , sizeof(visit));
for(i = ; i <= n ; i++)
{
d[i] = INF;
for(j = ; j <= n ; j++)
maps[i][j] = INF;
}
} void Dij()
{
int i, j, x, min;
d[] = ;
for(i = ; i <= n ; i++)
{
min = INF;
for(j = ; j <= n ; j++)
{
if(!visit[j] && d[j] < min)
{
min = d[j];
x = j;
}
}
visit[x] = ;
for(j = ; j <= n ; j++)
{
if(!visit[j] && d[j] > d[x] + maps[x][j])
d[j] = d[x] + maps[x][j];
}
}
} int main()
{
int i, a, b, c;
while(scanf("%d%d", &n, &m), m + n != )
{
Init();
for(i = ; i <= m ; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b] = min(maps[a][b], c);
maps[b][a] = maps[a][b];
}
Dij();
printf("%d\n", d[n]);
}
return ;
}//单元最短路 //方法2:弗洛伊德算法 #include<stdio.h>
#include<string.h>
#define N 110
#define INF 0xfffffff
#define min(a, b) (a < b ? a : b) int maps[N][N];
int n, m; void Init()
{
int i, j;
for(i = ; i <= n ; i++)
for(j = ; j <= n ; j++)
maps[i][j] = INF;
} void Floyd()
{
int i, k, j;
for(k = ; k <= n ; k++)
{
for(i = ; i <= n ; i++)
{
for(j = ; j <= n ; j++)
maps[i][j] = min(maps[i][j], maps[i][k] + maps[k][j]);
}
}
} int main()
{
int a, b, c, i;
while(scanf("%d%d", &n, &m), m + n != )
{
Init();
for(i = ; i <= m ; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[a][b] = min(maps[a][b], c);
maps[b][a] = maps[a][b];
}
Floyd();
printf("%d\n", maps[][n]);
}
return ;
}//多元最短路

HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544的更多相关文章

  1. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  2. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  3. HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 题意: 一个字符串,有多少个subsequence是回文串. 别人的题解: 用dp[i][j]表示这一段里 ...

  4. 待补 http://acm.hdu.edu.cn/showproblem.php?pid=6602

    http://acm.hdu.edu.cn/showproblem.php?pid=6602 终于能够看懂的题解: https://blog.csdn.net/qq_40871466/article/ ...

  5. HDU-1257 导弹拦截系统 http://acm.hdu.edu.cn/showproblem.php?pid=1257

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  6. http://acm.hdu.edu.cn/showproblem.php?pid=2579

    #include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1 ...

  7. KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594

    riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)但:aaaa与aa拼接后aa ...

  8. HDU1973 http://acm.hdu.edu.cn/showproblem.php?pid=1973

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<queue> #inc ...

  9. HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...

随机推荐

  1. R语言多重共现性的检测

    1.kappa值 2. library(car)vif(lm.sol) 得到各个系数的方差膨胀因子,当0<VIF<10的时候,不存在多重共线性,当10<=VIF<100,存在较 ...

  2. HDU 2149 (巴什博奕) Public Sale

    没什么好说的,一道水题. #include <cstdio> int main() { int n, m; ) { if(n <= m) { for(int i = n; i < ...

  3. Python 删除 数组

    numpy删除一列 从0开始,第三个参数是第几个维度  可以多删几个 

  4. php的webservice的soapheader认证问题

    参数通过类传输:class authentication_header {       private $username;       private $password;       public ...

  5. vm虚拟机挂载usb

    首先得保证能在VM里看到usb设备,如图

  6. windows下mysql 数据库的导入导出

    1.以.sql方式方式导入导出 http://www.360doc.com/content/11/0114/11/2905268_86441355.shtml 2.以.txt方式导入导出 http:/ ...

  7. 几种ESB(企业服务总线)介绍

    ESB(Enterprise Service Bus,即企业服务总线)是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素. 企业服务 ...

  8. Cent OS5.2安装Hyper-V集成光盘

    一.Hyper-V安装windows系统没有问题,windows2000以后系统都可以,一切顺利. 驱动程序:IDE.SCSI.网络.视频和鼠标 要想实现更强的功能,宿主机需要安装Hyper-V集成光 ...

  9. 发现第三方资源,chrome控制台

    for(var i=0,tags=document.querySelectorAll('iframe[src],frame[src],script[src],link[rel=stylesheet], ...

  10. 使用FTP搭建YUM

    VSFTP搭建YUM源 1.安装FTP [root@FTP kel]# rpm -qa |grep vsftp vsftpd-2.2.2-6.el6_0.1.x86_64 首先需要安装的ftp软件为v ...