POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 46727 | Accepted: 15899 |
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.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
Sample Input
5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100
Sample Output
90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
Dijkstra()
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; typedef __int64 LL; const int maxn = 2005; const int INF = 0x3f3f3f3f; struct Edge{ int u,v,next; LL w; bool operator < (const Edge & a)const { return w > a.w; } }edge[maxn<<1] ; int tot = 0,head[maxn]; bool vis[maxn]; LL dis[maxn]; void addedge(int u,int v,LL w) { edge[tot] = (Edge){u,v,head[u],w }; head[u] = tot++; } void Dijkstra() { priority_queue<Edge>que; Edge p; memset(dis,INF,sizeof(dis)); memset(vis,false,sizeof(vis)); p.v = 1; que.push(p); dis[1] = 0; while (!que.empty()) { p = que.top(); que.pop(); int u = p.v; if (vis[u]) continue; vis[u] = true; for (int i = head[u];i != -1;i = edge[i].next) { int v = edge[i].v; if (dis[u] + edge[i].w < dis[v]) { dis[v] = dis[u] + edge[i].w; p.u = u,p.v = v,p.w = dis[v]; que.push(p); } } } } int main() { //freopen("input.txt","r",stdin); int T,N,u,v; LL w; memset(head,-1,sizeof(head)); scanf("%d%d",&T,&N); for (int i = 0;i < T;i++) { scanf("%d%d%I64d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } Dijkstra(); printf("%I64d\n",dis[N]); return 0; }
spfa()
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int MAX_N = 1005; bool flag[MAX_N]; int edge[MAX_N][MAX_N]; void spfa(int n) { int dis[MAX_N]; queue<int>que; memset(flag,false,sizeof(flag)); memset(dis,0x3f3f3f3f,sizeof(dis)); dis[1] = 0; que.push(1); flag[1] = true; while (!que.empty()) { int curval = que.front(); que.pop(); flag[curval] = false; for (int i = 1;i <= n;i++) { if (dis[curval] < dis[i] - edge[curval][i]) { dis[i] = dis[curval] + edge[curval][i]; if (!flag[i]) { que.push(i); flag[i] = true; } } } } printf("%d\n",dis[n]); } int main() { int N,T; while (~scanf("%d%d",&T,&N)) { int u,v,w; for (int i = 1;i <= N;i++) { for (int j = 1;j <= i;j++) { if (i == j) edge[i][j] = 0; else edge [i][j] = edge[j][i] = INF; } } for (int i = 0;i < T;i++) { scanf("%d%d%d",&u,&v,&w); /*if (w < edge[u][v]) { edge[u][v] = edge[v][u] = w; }*/ edge[u][v] = edge[v][u] = min(w,edge[u][v]); } spfa(N); } return 0; }
POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)的更多相关文章
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- 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 (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 ...
随机推荐
- SAP CRM 在Web UI中创建搜索帮助
多数情况下,在Web UI为一个特定的字段提供搜索帮助需要在事务SE11中创建搜索帮助. (注:也可以通过在SE24中创建一个类并实现实现IF_BSP_WD_CUSTOM_F4_CALLBACK接口来 ...
- 用 Excel 测试“绘制两点间连线”的算法
最近在研究和制作数字示波器,其中涉及一个小算法:需要将 ADC 采样的数值在 TFT LCD 屏幕上面显示并且用“线”连接起来. ADC 按照时序对输入电压采样后,记录的是一个个的数值,如果显示的时候 ...
- C#使用ADO.NET访问数据库(一)
博主好久没更新博客了,最近有点忙(打麻将0.0..),今天更新一篇C#的,我还是想坚持更新博客,分享一下自己的心得,闲话少说,开始正题~~ ADO.NET概述:ADO.NET的作用在于他是客户端访问服 ...
- MySQL 导出数据
MySQL中你可以使用SELECT...INTO OUTFILE语句来简单的导出数据到文本文件上. 使用 SELECT ... INTO OUTFILE 语句导出数据 以下实例中我们将数据表 cnbl ...
- JAVA UUID 生成
UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成UUID的API.UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址.纳秒级时间.芯 ...
- 基于UDP的网络编程
与TCP编程相比较,UDP缺少了connect().listen()及accept()函数,这是由于UDP协议无连接的特性,不用维护TCP的连接.断开等状态. UDP编程框图 API函数 socket ...
- Hibernate入门笔记
相关基础概念请从其它教材简单了解,这里仅记录下第一个Hibernate程序的实现步骤. 环境说明: java开发工具:eclipse MARS.2 Release(4.5.2) hibernate版本 ...
- 【转】XenServer体系架构解析
XenServer是一套已在云计算环境中经过验证的企业级开放式服务器虚拟化解决方案,可以将静态.复杂的IT环境转变为更加动态.易于管理的虚拟数据中心,从而大大降低数据中心成本.同时,它可以提供先进的管 ...
- 关于IOS调用微信支付jsapi不起作用的解决方法
微信支付时,安卓机调用 jsapi可以支付,IOS就不行,点击立即支付,直接返回原立即支付页面,跟刷新页面差不多,解决方案很简单:两句话而已. 不得不说,微信支付坑太多了,我擦..... <sc ...
- BZOJ4300绝世好(傻)题
Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...