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

见代码:

 #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. App自适应

    http://blog.csdn.net/newjueqi/article/details/42779221

  2. ElasticSearch使用IK中文分词---安装步骤记录

    提示1:必须保证之前的ES中不存在index, 否则ES集群无法启动, 会提示red! 提示2:下载的IK如果太新,会报错 TokenStream被重载Caused by: java.lang.Ver ...

  3. JS面向(基于)对象编程--三大特征

    抽象 在讲解面向对象编程的三大特征前,我们先了解什么叫抽象,在定义一个类时候,实际上就是把一类事物的共有的属性和行为提取出来,形成一个物理模型(模板).这种研究问题的方法称为抽象. 封装 什么是封装? ...

  4. js中几个正则表达式相关函数使用时g标志的作用

    首先,javascript中涉及到正则表达式的函数总共有6个,可分为两种: 1.第一种是作为字符串对象的方法,即以 String.fun(); 形式调用,这里包括 split.search.match ...

  5. Java-马士兵设计模式学习笔记-建造者模式

    一.概述 二.代码 1.Animal.java public interface Animal { public void bark(); } 2.Dog.java public class Dog ...

  6. sin=in.readLine();

    import java.io.*; public class LineIO{ public static void main(String[] args) { String sin,inputStri ...

  7. Mysql笔记——触发器简单实例

    首先贴上触发器语法吧: CREATE TRIGGER <触发器名称> –触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象. { BEFOR ...

  8. PHPCMS V9添加模板自定义全局变量

    在我们使用PHPCMS V9的制作网站模板的时候,使用全局模板变量能轻松调用,使用起来非常方便,而且可以统一修改,方便维护. 下面就来讲一下在PHPCMS V9中如何添加自定义全局变量. 修改网站sy ...

  9. ES6入门之Generator函数

    Generator Generator函数是ES6提供的一种异步编程解决方案,Generator函数是一个状态机,封装了多个内部状态. 执行Generator函数会返回一个遍历器对象,也就是说,Gen ...

  10. c# 计算1-100之间的所有整数的和

    求1-100所有整数和: class Program { static void Main(string[] args) { ,); Console.WriteLine("1-100之间所有 ...