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. HDFS源码分析EditLog之读取操作符

    在<HDFS源码分析EditLog之获取编辑日志输入流>一文中,我们详细了解了如何获取编辑日志输入流EditLogInputStream.在我们得到编辑日志输入流后,是不是就该从输入流中获 ...

  2. Hive报错:Failed with exception Unable to rename

    之前也安装过hive,操作过无数,也没发现什么错误,今天因为之前安装的hadoop不能用了,不知道为什么,老是提示node 0,所以重新安装了hadoop和hive.安装完测试hive创建表也没发现什 ...

  3. java 泛型小小的测试题

    判断以下哪种书写时正确的? 1.ArrayList<String> lists = new ArrayList<String>();2.ArrayList<Object& ...

  4. Linux RabbitMQ的安装、环境配置、远程访问 , Windows 下安装的RabbitMQ远程访问

    Linux  RabbitMQ的安装和环境配置 1.安装 RabbitMQ是使用Erlang语言编写的,所以安装RabbitMQ之前,先要安装Erlang环境 #对原来的yum官方源做个备份 1.mv ...

  5. CentOS、乌班图设置固定静态IP

    CentOS.乌班图设置固定静态IP 一.centOS 1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2,在文件 ...

  6. 图床QAQ

  7. 在RedHat Linux系统中安装和配置snmp服务

    检查系统是否安装snmp服务 # rpm -qa|grep snmp net-snmp-5.3.2.2-17.el5 net-snmp-perl-5.3.2.2-17.el5 net-snmp-dev ...

  8. ERR:/usr/local/lib/libcrypto.so.1.0.0: no version information available

    解决方法: locate libssl.so.1.0.0   sudo rm /usr/local/lib/libssl.so.1.0.0   sudo ln -s /lib/x86_64-linux ...

  9. CentOS下配置静态IP

    第一.在VMware中进行配置使用桥接的方式.点击编辑选择虚拟网络编辑器 选择桥接模式,选择桥接到外部的网卡.选择我们主机下的网卡 第二步.配置虚拟机使用桥接模式 第三步:启动虚拟机进入到 /etc/ ...

  10. C#winform的datagridview设置选中行

    this.dataGridView1.CurrentCell = this.dataGridView1[colIndex, rowIndex];this.dataGridView1.BindingCo ...