//代码:

//方法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. bzoj2119

    题意就是差分后求形如ABA的串的个数,B的长度为M 这是2011国家集训队互测的试题,是道好题,我直接给出出题人的题解吧: 对于这种在线性序列上的组合计数问题,我们很容易想到一个工具:分治!分治算法在 ...

  2. HDU 4023 (博弈 贪心 模拟) Game

    如果硬要说这算是博弈题目的话,那这个博弈是不公平博弈(partizan games),因为双方面对同一个局面做出来的决策是不一样的. 我们平时做的博弈都是公平博弈(impartial games),所 ...

  3. codeforces 450 B Jzzhu and Sequences

    题意:给出f1=x,f2=y,f(i)=f(i-1)+f(i+1),求f(n)模上10e9+7 因为 可以求出通项公式:f(i)=f(i-1)-f(i-2) 然后 f1=x; f2=y; f3=y-x ...

  4. UVALive 4043 Ants 蚂蚁(二分图最佳完美匹配,KM算法)

    题意: 有n个蚂蚁n棵树,蚂蚁与树要配对,在配对成功的一对之间连一条线段,要求所有线段不能相交.按顺序输出蚂蚁所匹配的树. 思路: 这个题目真是技巧啊,不能用贪心来为每个蚂蚁选择最近的树,这样很可能是 ...

  5. HDU 1018 Big Number (阶乘位数)

    题意: 给一个数n,返回该数的阶乘结果是一个多少位(十进制位)的整数. 思路: 用对数log来实现. 举个例子 一个三位数n 满足102 <= n < 103: 那么它的位数w 满足 w ...

  6. Spring 事务配置5种方式

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  7. 关闭iptables(Centos)

    由于搭建了CDH-Hadoop,方便起见,事先关闭了防火墙: services iptables stop; chkconfig iptables off; services ip6tables st ...

  8. NodeJS模块

    node> module { id: 'repl', exports: { writer: { [Function: inspect] colors: [Object], styles: [Ob ...

  9. suse linux中apache+php服务器安装

    主站下载源码 tar zxvf httpd-2.2.4.tar.bz2cd httpd-2.2.4 ./configure --prefix=/usr/local/apache --sysconfdi ...

  10. JQuery开发之Galleriffic图片插件介绍

    Galleriffic是一个用于创建快速展示相册中照片的jQuery插件.从图一中可以看成,图片既可以以幻灯片的方式查看,也可以手动点击缩略图查看.Galleriffic还支持分页,从而使得它能够展示 ...