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 ...
随机推荐
- UVA - 10167 - Birthday Cake (简单枚举)
思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include &l ...
- HASH Partitioning--转载
原文地址:https://dev.mysql.com/doc/refman/5.1/en/partitioning-hash.html HASH Partitioning [+/-] 18.2.3.1 ...
- 轻松学习Linux之详解系统引导过程
轻松学习Linux之详解系统引导过程-1 轻松学习Linux之详解系统引导过程-2 本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- go timer
go timer package main import ( "fmt" "time" ) func debounce(interval time.Durati ...
- 设计模式六大原则(一):单一职责原则(Single Responsibility Principle)
单一职责(SRP)定义: 不要存在多于一个导致类变更的原因,通俗的说,即一个类只负责一项职责. 问题由来: 类T负责两个不同的职责:职责P1,职责P2.当由于职责P1需求发生改变而需要修改类T时,有可 ...
- Skill of vim
用vim也有一段时间了,谨以此记下一些有意思的技巧. 跳转 hjkl,左下右上. b/e跳转到上/下一个单词 ^/$跳转到行头/尾 从{,[,(,相应的},],)相互跳转.能够用% 跳转到局部变量的定 ...
- windows 批处理脚本(batch scripting)
Guide to Windows Batch Scripting DOS 不需对变量事先声明.未声明或未初始化变量是一个空字符串("") 1. 变量赋值 set命令用于变量赋值.s ...
- 2. Dubbo和Zookeeper的关系
转自:https://www.cnblogs.com/hirampeng/p/9540243.html Dubbo建议使用Zookeeper作为服务的注册中心. 1. Zookeeper的作用: ...
- ORACLE10g R2【RAC+ASM→RAC+ASM】
ORACLE10g R2[RAC+ASM→RAC+ASM] 本演示案例所用环境:RAC+ASM+OMF primary standby OS Hostname node1,node2 dgnode ...
- VUE错误记录 - 品牌后台练习 search(); 数组 splice forEach push 箭头函数
methods:{ add(){ var car = { id: this.id, name: this.name, ctime: new Date()}; this.list.push(car); ...