(简单) 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 ...
随机推荐
- JavaScript 常用功能实现一览(一)
摘自于网络:http://www.cnblogs.com/joinger/articles/1506482.html 适合阅读范围:对JavaScript一无所知-离精通只差一步之遥的人基础知识:HT ...
- Jenkins环境集成第一弹
1. 起因 策划经常过来让我打包给他们测试,过于频繁影响到了自己的进度,决定弄一个打包工具让他们自己打包,在网上搜索了一下貌似有几个比较成熟的工具,Travis,Jenkins等等. 在网上也搜索到了 ...
- PE文件格式详解(下)
作者:MSDN译者:李马 预定义段 一个Windows NT的应用程序典型地拥有9个预定义段,它们是.text..bss..rdata..data..rsrc..edata..idata..pdata ...
- Tree Cutting
Tree Cutting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Prob ...
- struts2获得提交是get还是post方法提交
String method=ServletActionContext.getRequest().getMethod(); System.out.println(method); 如果是get 会打印 ...
- Moya 浅析
Moya是一个高度抽象的网络库,他的理念是让你不用关心网络请求的底层的实现细节,只用定义你关心的业务.且Moya采用桥接和组合来进行封装(默认桥接了Alamofire),使得Moya非常好扩展,让你不 ...
- Job 逻辑执行图
General logical plan 典型的 Job 逻辑执行图如上所示,经过下面四个步骤可以得到最终执行结果: 从数据源(可以是本地 file,内存数据结构, HDFS,HBase 等)读取数据 ...
- 转:Selenium Grid+JAVA +Windows 配置(Selenium 2.0)
Selenium-Grid 允许你在多台机器的多个浏览器上并行的进行测试,也就是说,你可以同时运行多个测试.本质上来说就是,Selenium-Grid 支持分布式的测试执行.它可以让你的测试在一个分布 ...
- asp读取指定目录下的文件名
bianli(Server.MapPath("/")+"\pic") InStrRev("abcd.jpg", ".") ...
- aspx基础开始
<%@ Page Language="C#" Debug="true" trace="false" validateRequest=& ...