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. linux中likely()和unlikely()

    likely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢?首先明确: if (likely(value))等价于if (value) if (unlike ...

  2. C语言内存分配函数malloc——————【Badboy】

    C语言中经常使用的内存分配函数有malloc.calloc和realloc等三个,当中.最经常使用的肯定是malloc,这里简单说一下这三者的差别和联系. 1.声明 这三个函数都在stdlib.h库文 ...

  3. Android NDK开发初步

    在配置好NDK开发之后就能够使用C/C++开发android了.以下以一个HelloWorld项目来说明 1.新建一个Androidproject 新建一个HelloWorldproject 代码例如 ...

  4. 微信URL有效性验证

    1.填写配置项:填写 URL 和 Token 点击提交按钮  微信服务器会以get方式请求到所指定的URL,在此URL中进行URL的有效性验证 2.URL有效性的验证: if ("get&q ...

  5. Django框架ORM常用参数汇总_模型层

    primary_key 如果为True,那么这个字段就是模型的主键. 如果你没有指定任何一个字段的primary_key=True, Django就会自动添加一个IntegerField字段做为主键, ...

  6. 推荐20个非常有帮助的web前端开发教程

    1. CSS Vocabulary 一个伟大的指向和点击的小应用程序,让你加高速度掌握 CSS 语法的各个不同部分,学习各个属性的正确的名称. 2. Liquidapsive 一个简单的信息化布局.通 ...

  7. oracle 11g ocr 冗余配置

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/royjj/article/details/30506343  oracle 11g ocr 冗余 ...

  8. PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)

    sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...

  9. wifi debug command

    ==================================================================================================== ...

  10. pinpoint本地开发-web模块

    web模块中的前端依赖会导致工程很难打包成功,对于这些,我们可以直接注释掉 比如: <plugin> <groupId>com.github.eirslett</grou ...