(简单) POJ 2387 Til the Cows Come Home,Dijkstra。
Description
Farmer John's field has N (2 <= N <= 1000)
landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the
apple tree grove in which Bessie stands all day is landmark N. Cows
travel in the field using T (1 <= T <= 2000) bidirectional
cow-trails of various lengths between the landmarks. Bessie is not
confident of her navigation ability, so she always stays on a trail from
its start to its end once she starts it.
Given the trails between the landmarks, determine the
minimum distance Bessie must walk to get back to the barn. It is
guaranteed that some such route exists.
#include<iostream>
#include<cstring>
#include<queue> using namespace std; ///////////////////////////////////////////////////////////////// const int MaxN=;
const int INF=10e8; struct Node
{
int v,val; Node(int _v=,int _val=):v(_v),val(_val) {}
bool operator < (const Node &a) const
{
return val>a.val;
}
}; struct Edge
{
int v,cost; Edge(int _v=,int _cost=):v(_v),cost(_cost) {}
}; vector <Edge> E[MaxN];
bool vis[MaxN]; void Dijkstra(int lowcost[],int n,int start)
{
priority_queue <Node> que;
Node qtemp;
int len;
int u,v,cost; for(int i=;i<=n;++i)
{
lowcost[i]=INF;
vis[i]=;
}
lowcost[start]=; que.push(Node(start,)); while(!que.empty())
{
qtemp=que.top();
que.pop(); u=qtemp.v; if(vis[u])
continue; vis[u]=; len=E[u].size(); for(int i=;i<len;++i)
{
v=E[u][i].v;
cost=E[u][i].cost; if(!vis[v] && lowcost[v]>lowcost[u]+cost)
{
lowcost[v]=lowcost[u]+cost;
que.push(Node(v,lowcost[v]));
}
}
}
} inline void addEdge(int u,int v,int c)
{
E[u].push_back(Edge(v,c));
} ///////////////////////////////////////////////////////////////// int ans[]; int main()
{
ios::sync_with_stdio(false); int N,T;
int a,b,c; cin>>T>>N; for(int i=;i<=T;++i)
{
cin>>a>>b>>c; addEdge(a,b,c);
addEdge(b,a,c);
} Dijkstra(ans,N,N); cout<<ans[]<<endl; 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 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- 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 ...
随机推荐
- ng-option小解
ng-option 有数组,对象两种情况,但目前只接触过数组 数组: label for value in array 分为一般数组和对象数组 一般数组: <select ng-model=&q ...
- MyBatis中Like语句使用方式
oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 SELECT * FROM user WHERE nam ...
- group_concat()函数总结
group_concat(),手册上说明:该函数返回带有来自一个组的连接的非NULL值的字符串结果.比较抽象,难以理解. 通俗点理解,其实是这样的:group_concat()会计算哪些行属于同一组, ...
- CentOS下载及版本选择-CentOS LiveCD、LiveDVD和BinDVD区别
1.CentOS系统镜像有两个,安装系统只用到第一个镜像即CentOS-6.x-i386-bin-DVD1.iso(32位)或者CentOS-6.x-x86_64-bin-DVD1.iso(64位), ...
- sql快速参考
SQL 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition ALTER TABLE A ...
- jquery 中 $('div','li')
要搞清楚$('div','li') 和 $('div , li') 和 $('div li') 区别$('div','li')是$(子,父),是从父节点里找子,而不是找li外面的div $('div ...
- 新手引导-ugui
http://www.unitymanual.com/thread-38287-1-1.html 我已经在 干货区发布了,所以 这里就记录一下地址,懒得再贴了 新年第一贴,大家 看完代码 ,是不是发现 ...
- C++的精髓——虚函数
虚函数为了重载和多态的需要,在基类中是由定义的,即便定义是空,所以子类中可以重写也可以不写基类中的函数! 纯虚函数在基类中是没有定义的,必须在子类中加以实现,很像java中的接口函数! 虚函数 引入原 ...
- HTML元素分类:块级元素 内联元素和内联块状元素
在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 1,块状元素 常用的块状元素有: <div>.<p>.<h1 ...
- HDFS在Linux下的命令
1.对hdfs操作的命令格式是 1.1hadoop fs -ls <path> 表示对hdfs下一级目录的查看 1.2 hadoop fs -lsr <path> 表示对hd ...