Til the Cows Come Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 46727   Accepted: 15899

Description

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

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

* Line 1: Two integers: T and N

* 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

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

Sample Output

90

Hint

INPUT DETAILS:

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)的更多相关文章

  1. 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 ...

  2. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  3. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  4. 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 ...

  5. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  6. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  7. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  8. 怒学三算法 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 ...

  9. 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 ...

随机推荐

  1. 基于Cesium1.26地图API下的GeoServer2.9.0服务器跨域设置

    遇到的问题: 最近基于Cesium来做3D模型的地图开发,在访问自己发布的WMS服务之后,遇到了GeoServer跨域问题. 调用这个WMS服务的时候,浏览器(我用Chrome)开发者工具报错: 控制 ...

  2. HTML学习(一)基础篇

    这篇文章有人比我总结的好,适用于新手,我就适当的铺垫一下,结尾处会给你们网站,我就不班门弄斧了. 一)HTML结构 1.<head>标签 <title> <base/&g ...

  3. 记处理线上记录垃圾日志 The view 'Error' or its master was not found

    最近监控线上日志,网站是ASP.NET MVC 开发的,发现不少错误日志都记录同样的内容: The view 'Error' or its master was not found or no vie ...

  4. Android游戏开发实践(1)之NDK与JNI开发01

    Android游戏开发实践(1)之NDK与JNI开发01 NDK是Native Developement Kit的缩写,顾名思义,NDK是Google提供的一套原生Java代码与本地C/C++代码&q ...

  5. WKWebView浅析

    原文链接:supermokey WKWebView 一个WKWebView对象展示交互的web内容,例如应用于app内的浏览器.你可以在你的App中使用WKWebView. 综述 Important: ...

  6. ASP.NET MVC 3 网站优化总结(六)压缩 HTML

    压缩 html 可以去除代码中无用的空格等,这样可提高网站的加载速度并节省带宽.今天就让我们看看在 ASP.NET MVC 3 怎么实现 html 压缩,我们可通过实现 ActionFilter 来完 ...

  7. (八)数据呈现——一图胜千言<完结>

    数据分析师就像厨师一样.厨师的工作有5步:下单.备料.切配.烹饪.打荷.数据分析师的工作也有5步.呈现数据就好像打荷.厨师在把菜肴端给客人之前要做盘饰美化,让菜肴精致美观,这个工作就是打荷.同样,数据 ...

  8. how2heap分析系列:0

    新学期到了,给学弟们写点东西, https://github.com/shellphish/how2heap 这个how2heap挺不错的,讲述了heap上几种不同的漏洞利用技术,在后面发的几篇中我会 ...

  9. Context值和bool开关

      Context值和bool开关的相关内容 Context值分为2种 系统默认的context值 服务的context值 Context值的作用 主要是防止有未知文件进入目录文件之中(如将病毒拷贝到 ...

  10. windows10下sql server 2005 无法运行或sql server服务无法启动的完美解决方案

    问题:升级windows10后,sql server 2005 无法运行或sql server服务&sql server agent无法启动,如下图,怎么办? 一般情况下,我们第一反应就是sq ...