解题思路:最短路的模板题,注意一个细节处理即可。

见代码:

 #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的更多相关文章

  1. 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 ...

  2. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  3. (Dijkstra) POJ2387 Til the Cows Come Home

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 81024   Accepted ...

  4. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  5. 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 ...

  6. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

  7. 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 ...

  8. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  9. POJ2387 Til the Cows Come Home【Kruscal】

    题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...

随机推荐

  1. (转)两分钟彻底让你明白Android Activity生命周期(图文)!

    转自: http://blog.csdn.net/android_tutor/article/details/5772285 大家好,今天给大家详解一下Android中Activity的生命周期,我在 ...

  2. HDU5569/BestCoder Round #63 (div.2) C.matrix DP

    matrix Problem Description Given a matrix with n rows and m columns ( n+m is an odd number ), at fir ...

  3. oracle 密码忘记 找回密码

    生活中,容易忘记Oracle数据库system用户的密码,怎么办呢,小生带你一步步重新登上Oracle ,及时你密码忘记了. 1.打开cmd窗口,输入 sqlplus / as sysdba 2.运行 ...

  4. a cold welcome

    What does 'a cold welcome' refer to? On wednesday evening,we went to the town hall. It was the last ...

  5. lintcode:打劫房屋

    题目 打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动 ...

  6. [转]C:int型指针

    开源中国:http://my.oschina.net/lotte1699/blog/142538 网页快照:http://www.piaocafe.com/295977937/139381567037 ...

  7. MyBatis实现SaveOrUpdate

    这篇文章主要讲如何通过xml方式实现SaveOrUpdate,但是仍然建议在Service中实现. 例子 <insert id="saveOrUpdate" > < ...

  8. 深度学习框架Caffe —— Deep learning in Practice

    因工作交接需要, 要将caffe使用方法及整体结构描述清楚. 鉴于也有同学问过我相关内容, 决定在本文中写个简单的tutorial, 方便大家参考. 本文简单的讲几个事情: Caffe能做什么? 为什 ...

  9. !! Android developer 最新国内镜像

    Android developer 最新国内镜像: http://wear.techbrood.com, 包含Android最新文档以及Android Wear,Android TV,Android ...

  10. allegro使用汇总 [转贴]

    1.如何在allegro中取消花焊盘(十字焊盘) set up->design parameter ->shape->edit global dynamic shape parame ...