poj2387 Til the Cows Come Home
解题思路:最短路的模板题,注意一个细节处理即可。
见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = ;
int vis[maxn], w[maxn][maxn], d[maxn], t, n; void Dijkstra()
{
for(int i = ; i <= n; i++) d[i] = w[i][n];
for(int i = ; i <= n; i++)
{
int k = -;
int min1 = inf;
for(int j = ; j <= n; j++)
{
if(vis[j]) continue;
if(d[j] < min1)
{
min1 = d[j];
k = j;
}
}
if(k == -) break;
vis[k] = ;
for(int j = ; j <= n; j++)
{ if(vis[j]) continue;
if(d[j] > d[k] + w[k][j] && w[k][j] < inf)
{
d[j] = d[k] + w[k][j];
}
}
}
return ;
} int main()
{
int x, y, len;
while(~scanf("%d %d", &t, &n))
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
if(i == j) w[i][j] = ;
else w[i][j] = w[j][i] = inf;
}
memset(vis, , sizeof(vis));
//for(int i = 1; i <= n; i++) d[i] = inf;
//d[n] = 0;
while(t-- )
{
scanf("%d %d %d", &x, &y, &len);
//w[x][y] = w[y][x] = len; 刚开始没有进行if判断
if(w[x][y] > len) w[y][x] = w[x][y] = len;
}
Dijkstra();
printf("%d\n", d[]);
}
return ;
}
poj2387 Til the Cows Come Home的更多相关文章
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- poj2387 Til the Cows Come Home 最短路径dijkstra算法
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- poj2387 Til the Cows Come Home(Dijkstra)
题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...
- POJ-2387 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- POJ2387 Til the Cows Come Home【Kruscal】
题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...
随机推荐
- STL中set底层实现方式
Q:STL中set底层实现方式? 为什么不用hash? A: 第一个问题:set底层实现方式为RB树(即红黑树). 第二个问题: 首先set,不像map那样是key-value对,它的key与valu ...
- 网络打洞(P2P软件穿透内网进行通信) 原理
http://www.cnblogs.com/gansc23/archive/2010/10/20/1857066.html 首先先介绍一些基本概念:NAT(Network Address Trans ...
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等
http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...
- AA投资
AA投资创建于2015年,总部位于北京,创始人成妙绮和王浩泽,专注于天使轮的技术创新驱动的TMT项目投资. 投资方向 AA投资是一家2015年才成立的风险投资机构,专注于种子轮.天使轮.Pre-A轮的 ...
- Item2 + zsh
转自 http://11ten.gitcafe.io/book-a/iTerm2/index.html iTerm2的主要特点: 开源免费. 兼容性比默认Terminal更好.对于经常要远程使用的情况 ...
- CentOS配置SSH单向无密码访问
最近在研究一款文件系统,需要远程给客户机安装软件,且需要无SSH密码访问,另外需要远程给客户机传文件,每次输入root密码很不方便,就想到用ssh key生成公钥.私钥来验证,而避免每次就必须输入ro ...
- java Comparable和Comaprator的对比
Comparable使一个class具备不同实例间进行比较的行为.这些对象的集合,可作为Collections.sort或Arrays.sort的参数 Comparator可以看成一种算法的实现,将算 ...
- php mysql_insert_id()
mysql_insert_id mysql_insert_id()返回给定的 link_identifier中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号.如果没有指 ...
- WebStorm 对 Mocha 完美支持。
如果你在使用 Mocha 的话,请使用 WebStorm 来运行 mocha,Webstorm可以清晰查看每个 test 的详细信息,非常直观,强烈推荐.
- 【Tech】Ganglia安装配置
基础配置: Hadoop 2.2.0,Hbase 0.96. 四台集群机器,一台master,三台slave. 三台slave上分别装gmond:namenode机器上设置datasource. 客户 ...