POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387
题目大意:
给定无向图,要求输出从点n到点1的最短路径。
注意有重边,要取最小的。
水题。。对于无向图,从1到n和n到1是一样的。
直接Dijkstra即可
#include<cstdio>
#include<cstring>
const int MAXN=1000+10;
const int INF=999999;
int map[MAXN][MAXN];
int dis[MAXN];
int main()
{
int n,t;
while(~scanf("%d%d",&t,&n))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
map[i][j]=INF; dis[i]=INF;
} int from,to,len;
for(int i=0;i<t;i++)
{
scanf("%d%d%d",&from,&to,&len);
if(len < map[from][to])
map[from][to]=map[to][from]=len;
}
//Dijkstra
bool vis[MAXN]={0}; int cur=n; //这里如果为1,那么输出改为dis[n]
vis[cur]=true;
dis[cur]=0;
for(int i=0;i<n;i++)
{
for(int j=1;j<=n;j++)
{
if( map[cur][j]!=INF && dis[cur]+ map[cur][j] < dis[j])
dis[j]=dis[cur]+ map[cur][j];
} int min=INF;
for(int j=1;j<=n;j++)//find the next minimum
{
if( !vis[j] && dis[j] < min)
min=dis[cur = j];
}
vis[cur]=true;
} printf("%d\n",dis[1]);
}
}
POJ 2387 Til the Cows Come Home (Dijkstra)的更多相关文章
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
随机推荐
- SQL优化工具SQLAdvisor使用(转)
一.简介 在数据库运维过程中,优化SQL是业务团队与DBA团队的日常任务.例行SQL优化,不仅可以提升程序性能,还能够降低线上故障的概率. 目前常用的SQL优化方式包括但不限于:业务层优化.SQL逻辑 ...
- Android之——短信的备份与还原
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47091281 眼下,Android手机中的一些软件能够实现手机短信的备份与还原操作 ...
- java初探秘之推断输入的一串字符是否全为小写字母
import java.io.IOException; import java.util.*; public class Two { public static void main(String[] ...
- 74.sscanf数据扫描
"%[0-9A-Za-z] 读取一个集合,遇到不是数组或者大小写字母跳出 %*[^0-9A-Za-z]读取所有的非数字字母的字符,忽略 示例: ]= "123sadsadasd ...
- 69.fprintf fscanf
fprintf //从读文件中提取字符串到info1.user和info1.password中 fscanf(pfr, "%s%s", info1.user, info1.pass ...
- MSSQL相关用法
一.分页查询 方式一(row_number): SELECT TOP pageSize * FROM (SELECT row_number() OVER (ORDER BY orderColumn) ...
- Mysql多实例安装+主从复制+读写分离 -学习笔记
Mysql多实例安装+主从复制+读写分离 -学习笔记 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px ...
- BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
继上一节.BeautifulSoup的高级应用 之 find findAll,这一节,主要解说BeautifulSoup有关的其它几个重要应用函数. 本篇中,所使用的html为: html_doc = ...
- tomcat的一些简单配置
一.管理tomcatusernamepassword conf文件夹下,tomcat-users.xml <span style="font-size:24px;">& ...
- mysql测试spring事务是否生效
同时对三张表进行插入操作,事务保证完整性.下面进行简单测试: 1. 锁定表 锁定用户表 LOCK TABLES user WRITE; 查看表是否锁定: show ; 显示被锁定的表. 2. 验证在同 ...