题目链接: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. ubuntu 安装xdebug

    Add XDebug to Ubuntu 14.04 Submitted by Wilbur on Tue, 06/17/2014 - 12:49pm It's pretty easy to add ...

  2. codesandbox

    codesandbox https://codesandbox.io https://codesandbox.io/dashboard https://codesandbox.io/dashboard ...

  3. js滚动大于多少菜单就开始固定

    //导航置顶 $(window).scroll(function () { var pos = $(window).scrollTop(); ) { $("#menu").addC ...

  4. Andorid API Package --->android.animation

    包名: android.animation                                Added in API level 11 URL:http://developer.andr ...

  5. BZOJ2555:SubString——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2555 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作 ...

  6. BZOJ2668:[CQOI2012]交换棋子——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2668 https://www.luogu.org/problemnew/show/P3159#sub ...

  7. [Leetcode] word search 单词查询

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. 洛谷 P1311 选择客栈 解题报告

    P1311 选择客栈 题目描述 丽江河边有 \(n\) 家很有特色的客栈,客栈按照其位置顺序从 \(1\) 到 \(n\) 编号.每家客栈都按照某一种色调进行装饰(总共 \(k\) 种,用整数 \(0 ...

  9. android getpost代码

    GetPostUtil public class GetPostUtil { /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param par ...

  10. 【DP】【P2340】奶牛会展

    传送门 Description 奶牛想证明它们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或 ...