poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接: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算法)的更多相关文章
- 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 ...
- POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 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 ...
- 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 ...
随机推荐
- ZOJ 1913 J-Eucild's Game
https://vjudge.net/contest/67836#problem/J Two players, Stan and Ollie, play, starting with two natu ...
- 安装django 提示ImportError: No module named setuptools
安装django前要先安装setuptools 先安装一些必要的包,否则会报错:Python build finished, but the necessary bits to build these ...
- 关于c++中public & private方法调用问题
class IDNoIdentifier { public: IDNoIdentifier(); ~IDNoIdentifier(); typedef vector<cv::Rect> C ...
- jquery animate() stop() finish() 方法使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Elasticsearch 中文分词器IK
1.安装说明 https://github.com/medcl/elasticsearch-analysis-ik 2.release版本 https://github.com/medcl/elast ...
- BZOJ3197:[SDOI2013]刺客信条——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3197 故事发生在1486 年的意大利,Ezio 原本只是一个文艺复兴时期的贵族,后来因为家族成员受 ...
- HDU4757:Tree——题解
http://acm.hdu.edu.cn/showproblem.php?pid=4757 给一棵有点值的树,每次询问u~v的最短路当中的一个点的点权异或z最大值. 前置技能:HDU4825 前置技 ...
- [LOJ10186]任务安排
link 试题分析 一道斜率优化的dp 易得方程$f[j]=(S+t[i])\times c[j]+f[i]-t[i]\times c[i]+s\times c[n]$,为什么要写成这要,因为这样其实 ...
- requireJs使用方法项目实例
首先,定义 main.js 和 事件处理的公共 js main.js 主要是定义引用名称和路径的对应关系 事件绑定模块: 写jsp页面: jsp中先引入 require.js 和 main.js 然 ...
- [codeforces/edu4]总结(F)
链接:http://codeforces.com/contest/612/ A题: 枚举切多少个p,看剩下的能否整除q. B题: 从1到n模拟一下,累加移动的距离. C题: 先用括号匹配的思路看是否有 ...