HUD.2544 最短路 (Dijkstra)
HUD.2544 最短路 (Dijkstra)
题意分析
- 1表示起点,n表示起点(或者颠倒过来也可以)
- 建立无向图
- 从n或者1跑dij即可。
代码总览
#include <bits/stdc++.h>
#define nmax 110
#define inf 1e8
using namespace std;
int mp[nmax][nmax];
int shortpath[nmax];
bool visit[nmax];
int n,m;
void dij(int s)
{
int cur = s;
memset(visit,0,sizeof(visit));
for(int i = 1;i<=n;++i) shortpath[i] = inf;
shortpath[cur] = 0;
visit[cur] = true;
for(int i = 1;i<=n ;++i){
for(int j = 1;j<=n;++j){
if(!visit[j] && shortpath[cur] + mp[cur][j] < shortpath[j]){
shortpath[j] = shortpath[cur] + mp[cur][j];
}
}
int nowmin = inf;
for(int j = 1; j<=n;++j){
if(!visit[j] && shortpath[j] <nowmin){
nowmin = shortpath[j];
cur = j;
}
}
visit[cur] = true;
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF){
if( n == 0 && m == 0) break;
int sta,ed,cost;
for(int i = 1;i<=n;++i){
for(int j = 1;j<=n;++j){
mp[i][j] = inf;
}
}
for(int i = 0; i<m;++i){
scanf("%d %d %d",&sta,&ed,&cost);
int temp = mp[sta][ed];
if(cost < temp){
mp[sta][ed] = mp[ed][sta] = cost;
}
}
dij(1);
printf("%d\n",shortpath[n]);
}
return 0;
}
HUD.2544 最短路 (Dijkstra)的更多相关文章
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- HDOJ/HDU 2544 最短路---dijkstra算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...
- HDU 2544最短路dijkstra模板题
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HUD 2544 最短路 迪杰斯特拉算法
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 2544 最短路(dijkstra+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- hdu 2544 最短路
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
随机推荐
- 一对多,多的逗号分隔存在新字段中(Group_concat 用法)
sql 语句: SELECT ( SELECT Group_concat(t_work_group_user.user_id) FROM ...
- Python零基础入门必知
Python自学知识点总结 //2018.10.09 1. Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido ...
- 【WXS】变量定义保留标识符
以下字符不能作为变量名称定义: delete void typeof null undefined NaN Infinity var if else true false require this f ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- NodeJs学习笔记01-你好Node
如果你对NodeJs略知一二,不禁会感叹,使用JS的语法和代码习惯就能开发一个网站的后台,实现复杂的数据交互,牛! 对于学习java和php就夹生的小码农来说,简直就是靡靡之音呐~~~ 今晚带着忐忑的 ...
- 从零开始的Python学习Episode 4——列表
一.列表 列表与数组相似,定义一个列表 a=[1,2,3,4,5] 1.基本操作 a=[1,2,3,4] #切片 范围取值时,包括第一项但不包括最后一项,顾头不顾尾 print(a[0:]) #从头到 ...
- 幸运的袋子(深度优先遍历(Depth First Search,DFS))
题目描述 一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的).如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积. 例如:如果袋子里面的球的号码是{1, 1, 2 ...
- ThinkPHP - 1 - 本地部署
ThinkPHP ThinkPHP是一个快速.简单的基于MVC和面向对象的轻量级PHP开发框架,遵循Apache2开源协议发布,从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时 ...
- 三:Fair Scheduler 公平调度器
参考资料: http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/FairScheduler.html http://h ...
- MyBatis 基本构成与框架搭建
核心组件 SqlSessionFactoryBuilder (构造器) 根据配置信息(eg:mybatis-config.xml)或者代码来生成SqlSessionFactory. SqlSessio ...