题目链接: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. 原生js 自定义confirm

    本文参考博客园另一篇文章:https://www.cnblogs.com/hzj680539/p/5374052.html,在此感谢. 在实际开发当中,考虑到原生js组件,包括alert.confir ...

  2. RT-thread内核之对象管理系统

    一.数据结构 1.对象控制块:在include/rtdef.h中定义 /** * Base structure of Kernel object */ struct rt_object { char ...

  3. 【题解】Atcoder AGC#16 E-Poor Turkeys

    %拜!颜神怒A此题,像我这样的渣渣只能看看题解度日╭(╯^╰)╮在这里把两种做法都记录一下吧~ 题解做法:可以考虑单独的一只鸡 u 能否存活.首先我们将 u 加入到集合S.然后我们按照时间倒序往回推, ...

  4. PHP检测json格式数据

    首先要记住json_encode返回的是字符串, 而json_decode返回的是对象 判断数据不是JSON格式: 复制代码代码如下: function is_not_json($str){      ...

  5. hdu 5625

    Clarke and chemistry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. Coconuts HDU - 5925 二维离散化 自闭了

    TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, ...

  7. POJ 3984 BFS

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20665   Accepted: 12100 Descriptio ...

  8. 轮廓算法的结果验证工具/How to validate the outline output

    因为轮廓算法的结果通过直接观察输出很难判断结果的正确性. 但是如果把输入和输出同时绘制出来,用眼睛判别则相对简单许多. 输入建筑的文件内容格式为,粗体格式为建筑高度: 10 110 5020 60 7 ...

  9. Lucene4.6至 Lucene6.6的每个迭代对API的改动

    由于项目需求,需要将Lucene4.6升级到Lucene6.6,因此我对这之间的所有重要的API改动做了搜集:特别重要的改变加粗显示. Lucene4.7改动: LUCENE-5405: Make S ...

  10. asp:DropDownList与asp:DataList的联合使用

    情况:当在asp:DropDownLis点击选取其中的一个值来响应datalist的值. <form id="form1" runat="server"& ...