(简单) 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 ...
随机推荐
- new thoughts over function pointers
Previous works do not relate to function pointers, but reading some documents reading and learning S ...
- webservice(soap)接口的加密,SHA-1实现
import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security. ...
- sql高级篇(一)
1.select top mysql中: select * from persons limit 5; 相当于oracle中的: select * from persons <=5; 在翻页中经 ...
- UVALive 2521 Game Prediction 题解
这个我上来把题目理解错了,我以为所有人的牌都是一样的,感觉这个题太麻烦了吧,而且题目样例过不去啊……后来发现理解错了,给出的数据是他一个人的数据,就是让我们求他一定能赢的轮数,所有的牌是固定的(1 - ...
- Mesos架构简介
1. 前言 同其他大部分分布式系统一样,Apache Mesos为了简化设计,也是采用了master/slave结构,为了解决master单点故障,将master做得尽可能地轻量级,其上面所有的元数据 ...
- Git学习 -- 删除文件
1 从版本库删除文件 git rm <file> git commit -m "xxx" 2 工作区中文件被误删,但版本库中没有删除,可以恢复到工作区 git chec ...
- jvm attach
http://ayufox.iteye.com/blog/655761 管道通信
- RasterBandClass Class
Product Availability Available with ArcGIS Engine, ArcGIS Desktop, and ArcGIS Server. Description Th ...
- 【学生成绩管理系统】 大二c语言作业
几年前写的了,只能在命令行窗口运行,虽然比较挫,还是有一定参考价值... #include <cstdio> #include <conio.h> #include <i ...
- 配置suricata
yum -y install libpcap libpcap-devel libnet libnet-devel pcre \ pcre-devel gcc gcc-c++ automake auto ...