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

见代码:

 #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://www.techweb.com.cn/network/system/2016-01-11/2256859.shtml http://www.woshipm.com/pmd/172952. ...

  2. zoj 3529 A Game Between Alice and Bob 博弈论

    思路:每个数的SG值就是其质因子个数,在进行nim博弈 代码如下: #include<iostream> #include<cstdio> #include<cmath& ...

  3. C之算法

       1° 选择排序算法    核心思路如下图: 以字符串排序进行说明 #include <stdio.h> #include <string.h> #define SIZE ...

  4. android-non-ui-to-ui-thread-communications-part-4-of-5

    In parts 1-3 of this series, I have explored three different means for an Android non-UI thread to c ...

  5. oci.dll文件是用来干嘛的? 如果没有安装ORACLE客户端提示oci.dll未加载

    oracle数据库开发编程中,没有找到oci.dll,一般是系统的 path 设置有问题, 查找oci.dll, 然后加入到系统路径.oci.dll 可下载解压到系统盘的system32目录下.然后打 ...

  6. CentOS系统配置redis

      1.切换到/usr/sr cd /usr/src wget http://download.redis.io/releases/redis-3.2.0.tar.gz   2.解压,安装 tar x ...

  7. React组件-mixin

    一.组件 二.代码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&q ...

  8. React-非dom属性-ref标签

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  9. 点击Button后,执行MouseDown的过程(使用Call Stack观察很清楚)

    Form1上放两个按钮Button1和Button2,默认输入焦点是Button1,现在点击Button2,产生WM_LBUTTONDOWN消息 procedure TForm1.Button2Mou ...

  10. LightOj 1245 --- Harmonic Number (II)找规律

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...