题目链接:http://poj.org/problem?id=2387

题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径。

注意的是先输入边,在输入的顶点数,不要弄反哦~~~

 #include <iostream>
#include <cstdio>
using namespace std;
int map[][],Min,node[],vis[],t,q;
const int INF=; void set()
{
for (int i=; i<=; i++)
{
node[i]=INF;
vis[i]=;
for (int j=; j<=; j++)
map[i][j]=INF;
}
} int dijkstra(int m)
{
int tm=m;
vis[m]=;
node[m]=;
for (int k=; k<=q; k++)
{
Min=INF;
for (int i=; i<=q; i++)
if (!vis[i])
{
if (node[i]>map[tm][i]+node[tm])
{
node[i]=map[tm][i]+node[tm];
//cout<<map[tm][i]<<" "<<node[i]<<endl;
}
if (Min>node[i])
{
//cout<<Min<<endl;
Min=node[i];
m=i;
}
}
vis[m]=;
tm=m;
}
return Min;
} int main ()
{
int n;
//memset(map,0,sizeof(map));
while (~scanf("%d%d",&t,&n))
{
q=;
set();
while (t--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if (q<a)
q=a;
else if (q<b)
q=b;
if (map[a][b]>c)
{
map[a][b]=map[b][a]=c;
}
}
dijkstra(n);
printf("%d\n",node[]);
}
return ;
}

poj 2387 Til the Cows Come Home(dijkstra算法)的更多相关文章

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

  2. POJ 2387 Til the Cows Come Home (Dijkstra)

    传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...

  3. Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)

    题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...

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

  5. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  6. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  7. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  8. 怒学三算法 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 ...

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

随机推荐

  1. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

  2. node 第一天

    在开发的过程中,几乎不可能一次性就能写出毫无破绽的程序,断点调试代码是一个普遍的需求. 作为前端开发工程师,以往我们开发的JavaScript程序都运行在浏览器端,利用Chrome提供的开发者工具就可 ...

  3. Delphi Dataset CurValue

    TField.CurValue Property Represents the current value of the field component including changes made ...

  4. table 标签 语法

  5. 客户端 new socket时候 就像服务端发起连接了

    客户端 new socket时候  就像服务端发起连接了

  6. HUAS 1477 经营与开发(贪心)

    考虑DP,令dp[i][j][k]当前在第i个星球,用了j次维修,k次开采后所获得的最大价值.复杂度为O(n^3).超时 如果我们发现,对于初始时能力值为w所能产生的最大价值y,初始时能力值为1所能产 ...

  7. Python 基本数据结构

    Python基本数据结构 数据结构:通俗点儿说,就是存储数据的容器.这里主要介绍Python的4种基本数据结构:列表.元组.字典.集合: 格式如下: 列表:list = [val1, val2, va ...

  8. [洛谷P1440]求m区间内的最小值

    题目大意:给你n个数,求出每个数前m位的最小值 题解:单调队列,用一个可以双向弹出的队列来存一串数,满足里面的数具有单调性,我们可以假设它是单调递增的,即求最小的数.那么可以把要插入的这个数与队尾元素 ...

  9. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  10. JavaScript是如何实现继承的

    JavaScript中的function是万能的,除了用于的函数定义,也可以用于类的定义.JavaScript的继承,说起来也是有点怪,没有public,private等访问控制修饰,也没有imple ...