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

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
注意:先输入边数后输入结点数,存在重边
#include"cstdio"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E;
int vis[MAXN];
int d[MAXN];
int dijkstra(int s)
{
for(int i=;i<=V;i++)
{
vis[i]=;
d[i]=mp[s][i];
}
vis[s]=; for(int i=;i<=V;i++)
{
int mincost,k;
mincost=INF;
for(int j=;j<=V;j++)
{
if(!vis[j]&&d[j]<mincost)
{
k=j;
mincost=d[j];
}
} vis[k]=;
for(int j=;j<=V;j++)
{
if(!vis[j]&&d[j]>d[k]+mp[k][j])
{
d[j]=d[k]+mp[k][j];
}
} }
return d[];
}
int main()
{
while(scanf("%d%d",&E,&V)!=EOF)
{
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
if(cost<mp[u][v]) mp[u][v]=mp[v][u]=cost;//存在重边
}
int ans=dijkstra(V);
printf("%d\n",ans);
}
return ;
}

堆优化的dijkstra

#include"cstdio"
#include"vector"
#include"queue"
using namespace std;
typedef pair<int,int> P;
const int MAXN=;
const int INF=0x3fffffff;
int mp[MAXN][MAXN];
int V,E;
vector<int> G[MAXN];
int d[MAXN];
void dijkstra(int s,int end)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P, vector<P>,greater<P> > que;
que.push(P(,s));
d[s]=; while(!que.empty())
{
P p=que.top();que.pop();
if(p.second==end)
{
printf("%d\n",p.first);
return ;
}
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++)
{
int to=G[v][i];
if(d[to]>d[v]+mp[v][to])
{
d[to]=d[v]+mp[v][to];
que.push(P(d[to],to));
}
}
}
}
int main()
{
while(scanf("%d%d",&E,&V)!=EOF)
{
for(int i=;i<=V;i++)
{
G[i].clear();
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
}
for(int i=;i<E;i++)
{
int u,v,cost;
scanf("%d%d%d",&u,&v,&cost);
G[v].push_back(u);
G[u].push_back(v);
if(cost<mp[u][v]) mp[v][u]=mp[u][v]=cost;
}
dijkstra(,V);
}
return ;
}

spfa+前向星可解决重边问题。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Edge{
int to,w,next;
}es[];
int head[MAXN],tot;
int n,m;
void addedge(int u,int v,int w)
{
es[tot].to=v;
es[tot].w=w;
es[tot].next=head[u];
head[u]=tot++;
}
int d[MAXN],vis[MAXN];
void spfa(int s)
{
for(int i=;i<=n;i++)
{
d[i]=INF;
vis[i]=;
}
queue<int> que;
que.push(s);
d[s]=;
vis[s]=;
while(!que.empty())
{
int u=que.front();que.pop();
vis[u]=;
for(int i=head[u];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.to]>d[u]+e.w)
{
d[e.to]=d[u]+e.w;
if(!vis[e.to])
{
que.push(e.to);
vis[e.to]=;
}
}
}
}
printf("%d\n",d[n]);
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-,sizeof(head));
tot=;
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
spfa();
}
return ;
}

Java版:

前向星+spfa

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Queue;
class Edge{
int to,w,net;
Edge(){}
Edge(int to,int w,int net)
{
this.to=to;
this.w=w;
this.net=net;
}
}
public class Main{
static final int MAXN=1005;
static final int INF=0x3f3f3f3f;
static int m,n;
static int[] head = new int[MAXN];
static Edge[] es = new Edge[4005];
static int tot;
static void addedge(int u,int v,int w)
{
es[tot] = new Edge(v,w,head[u]);
head[u] = tot++;
} static int[] d = new int[MAXN];
static boolean[] vis = new boolean[MAXN];
static int spfa(int src,int ter)
{
Arrays.fill(vis, false);
Arrays.fill(d, INF);
Queue<Integer> que = new LinkedList<Integer>();
que.add(src);
d[src]=0;
while(!que.isEmpty())
{
int u=que.peek();que.poll();
vis[u]=false;
for(int i=head[u];i!=-1;i=es[i].net)
{
Edge e = es[i];
if(d[e.to]>d[u]+e.w)
{
d[e.to]=d[u]+e.w;
if(!vis[e.to])
{
que.add(e.to);
vis[e.to]=true;
}
}
}
}
return d[ter];
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
tot=0;
Arrays.fill(head, -1);
m=in.nextInt();
n=in.nextInt();
for(int i=0;i<m;i++)
{
int u,v,w;
u=in.nextInt();
v=in.nextInt();
w=in.nextInt();
addedge(u,v,w);
addedge(v,u,w);
}
int res=spfa(n,1);
System.out.println(res);
}
}
}

POJ2387(最短路入门)的更多相关文章

  1. 图论:HDU2544-最短路(最全、最经典的最短路入门及小结)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. POJ - 2387 Til the Cows Come Home (最短路入门)

    Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before ...

  3. poj2387 最短路

    题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: #include<stdio.h> #include<string. ...

  4. POJ1502(最短路入门题)

    MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7471   Accepted: 4550 Des ...

  5. [原]最短路专题【基础篇】(updating...)

    hud1548 a strange lift  最短路/bfs  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:一个奇怪的电梯,每层楼的 ...

  6. 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)

    Dijkstra+ 链式前向星+ 优先队列   Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...

  7. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  8. POJ-2387(原始dijkstra求最短路)

    Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...

  9. poj2387 初涉最短路

    前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...

随机推荐

  1. 【Python基础】之不同的文件在不同目录下导入指定模块的方法

    如下图三个文件的目录路径 – project     |–  1     |    |–  2     |    |    |–  3    |    |    |    |– owen.py     ...

  2. Redis单台的安装部署及集群部署

    Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...

  3. 在mac下搭建java开发环境

    刚刚从windows系统转到使用mac系统.感觉不是特别熟悉,须要一定的适应时间. 以下简介一下mac下搭建主要的java开发环境. 1.安装jdk 安装jdk1.7后,发现不须要进行环境变量配置,直 ...

  4. IT痴汉的工作现状10-Sprint Planning

    这是我们的第四个Sprint了.因为上一个迭代周期的失利,Leader群发邮件这样描写叙述道:"对任务的乐观预计,导致Sprint 3没有如期完毕. 我们须要在这次Sprint计划中细致评估 ...

  5. 安装配置Django

    安装配置Django 以下是基于python3.5 pip install Django 把python环境目录Scripts配置到环境变量,主要在命令行中随时可以使用django-admin 验证 ...

  6. 升级webapi依赖的Newtonsoft.json的版本(转)

    随着微软日渐重视开源社区的贡献,微软在自己的产品中往往也会集成开源的第三方库. 比如System.Net.Http.Foramatting.dll 就依赖于Newtonsoft.json v4.5. ...

  7. viewport详解

    本文主要讲解viewpor相关知识. 参考资料&内容来源 博客园:https://www.cnblogs.com/zaoa/p/8630393.html 博客园:http://www.cnbl ...

  8. BZOJ1217: [HNOI2003]消防局的设立

    BZOJ1217: [HNOI2003]消防局的设立 Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地. 起初为了节约材料,人类只修建了n-1条道路来连接这些基地 ...

  9. LeetCode:递增的三元子序列【334】

    LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i ...

  10. Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】

    本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少 ...