POJ2387(最短路入门)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 38556 | Accepted: 13104 |
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
注意:先输入边数后输入结点数,存在重边
#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(最短路入门)的更多相关文章
- 图论:HDU2544-最短路(最全、最经典的最短路入门及小结)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 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 ...
- poj2387 最短路
题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: #include<stdio.h> #include<string. ...
- POJ1502(最短路入门题)
MPI Maelstrom Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7471 Accepted: 4550 Des ...
- [原]最短路专题【基础篇】(updating...)
hud1548 a strange lift 最短路/bfs 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题意:一个奇怪的电梯,每层楼的 ...
- 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)
Dijkstra+ 链式前向星+ 优先队列 Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...
- 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 ...
- POJ-2387(原始dijkstra求最短路)
Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...
- poj2387 初涉最短路
前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...
随机推荐
- 【翻译自mos文章】关于分区索引:Global, Local, Prefixed and Non-Prefixed
来源于: Partitioned Indexes: Global, Local, Prefixed and Non-Prefixed (文档 ID 69374.1) APPLIES TO: Oracl ...
- 渐变背景(background)效果
chrom and Safari浏览器: webkit核心的浏览器.使用CSS3渐变方法(css-gradient) -webkit-gradient(type, start_point, end_p ...
- spring中bean的作用域属性single与prototype的区别
https://blog.csdn.net/linwei_1029/article/details/18408363
- 02 redis通用命令操作
set hi hello 设置值 get hi 获取值 keys * 查询出所有的key memcached 不能查询出所有的key keys *h 模糊查找key keys h[ie] 模糊查找 k ...
- matlab biplot 符号的困惑
在matlab中做Principal component Analysis 时,常要用biplot 函数来画图,表示原分量与主分量(principal component)之间的关系,以及原始观察数据 ...
- python 基础 1.5 python数据类型(四)--字典常用方法示例
一. 字典 #字典 dict1 = {'name':'lzc','age':'20','sex':'man'} print dict1 print type(dict1) >>> { ...
- with(nolock) 与 with(readpast) 与不加此2个的区别
调试窗口一: 或者查询窗口一: 总之:事务没有结束 查询窗口二:
- AWS:4.VPC
主要介绍 1.Amazon混合云 2.将EC2加入VPC 3.VPC经典场景 4.VPC安全保障 Amazon混合云 : 在公有云的基础上创建私有云 VPC概念 VPC(VPC Virtual Pri ...
- vsftp时间差8个小时的解决方法
$ vi /etc/vsftpd/vsftpd.conf use_localtime=YES ;
- 关于jquery-weui.js中时间控件datetimepicker的使用
今天第一次接触jquery-weui,不太了解用法,然而官方文档写的也很简略,只好打开源代码进行研究,我想要的是设置开始日期大于当前日期,然后在源码中发现有min这个默认为undefined的属性,于 ...