您的位置:首页 > 其它

# hihocoder #1093 : 最短路径·三:SPFA算法

2015-10-05 10:28 405 查看
标签(空格分隔): hihocoder

1093 : 最短路径·三:SPFA算法

  对于稀疏图,边的数目远小于N^2(其中,N指顶点数)。因此邻接矩阵中会有大量的“表示”此边不存在的元素,不仅浪费了空间,也降低了效率。这里参考《算法竞赛入门经典》中第11章用矩阵来表示邻接表(Adjacency List)。算法实现参照其11.2.4节。这种表示法,每个结点i都有一个链表,里面保存着从i出发的所有边。对于无向图来说,每条边会在邻接表中出现两次。

  这里用数组实现了链表:首先给每条边编号,然后用first[u]保存结点u的第一条边的编号,next[e]表示编号为e的边的“下一条边”的编号。

  SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的计算。算法大致流程是用一个队列来进行维护。初始时将起始点加入队列,每次从队列中取出一个元素,并与所有与它相邻的点进行松弛,若某个相邻的点松弛成功,则将其入队。直到队列为空时结束算法。(参考SPFA算法

题目:

Time Limit:10000ms

Case Time Limit:1000ms

Memory Limit:256MB

描述

万圣节的晚上,小Hi和小Ho在吃过晚饭之后,来到了一个巨大的鬼屋!

鬼屋中一共有N个地点,分别编号为1..N,这N个地点之间互相有一些道路连通,两个地点之间可能有多条道路连通,但是并不存在一条两端都是同一个地点的道路。

不过这个鬼屋虽然很大,但是其中的道路并不算多,所以小Hi还是希望能够知道从入口到出口的最短距离是多少?

提示:Super Programming Festival Algorithm。

输入

每个测试点(输入文件)有且仅有一组测试数据。

在一组测试数据中:

第1行为4个整数N、M、S、T,分别表示鬼屋中地点的个数和道路的条数,入口(也是一个地点)的编号,出口(同样也是一个地点)的编号。

接下来的M行,每行描述一条道路:其中的第i行为三个整数u_i, v_i, length_i,表明在编号为u_i的地点和编号为v_i的地点之间有一条长度为length_i的道路。

对于100%的数据,满足N<=10^5,M<=10^6, 1 <= length_i <= 10^3, 1 <= S, T <= N, 且S不等于T。

对于100%的数据,满足小Hi和小Ho总是有办法从入口通过地图上标注出来的道路到达出口。

输出

对于每组测试数据,输出一个整数Ans,表示那么小Hi和小Ho为了走出鬼屋至少要走的路程。

Sample Input

5 10 3 5

1 2 997

2 3 505

3 4 118

4 5 54

3 5 480

3 4 796

5 2 794

2 5 146

5 4 604

2 5 63

Sample Output

172

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
final static int MAXN = 100005;
final static int MAXM = 1000005;
final static int INF = 1000000005;

public static void main(String[] args) {
Scanner sin = new Scanner(System.in);
int N = sin.nextInt();
int M = sin.nextInt();
int st = sin.nextInt();
int ed = sin.nextInt();
sin.nextLine();

int[] first = new int[MAXN];
int[] ustpoint = new int[MAXM];
int[] venpoint = new int[MAXM];
int[] wlen = new int[MAXM];
int[] next = new int[MAXM];

for (int i = 0; i < N; i++) {
first[i] = -1;
}

int i = 0;
int cnt = 0;
while (i++ < M) {
int ct1 = sin.nextInt();
int ct2 = sin.nextInt();
int ct3 = sin.nextInt();
sin.nextLine();
// 无向图,每条边出现两次
ustpoint[cnt] = ct1;
venpoint[cnt] = ct2;
wlen[cnt] = ct3;
next[cnt] = first[ustpoint[cnt]];
first[ustpoint[cnt]] = cnt;
++cnt;
ustpoint[cnt] = ct2;
venpoint[cnt] = ct1;
wlen[cnt] = ct3;
next[cnt] = first[ustpoint[cnt]];
first[ustpoint[cnt]] = cnt;
++cnt;
}

LinkedList<Integer> queue = new LinkedList<Integer>();
queue.add(st);
boolean[] inq = new boolean[MAXN];
for(int j = 1; j < MAXN; j++) {
inq[j] = false;
}
int[] dis = new int[MAXN];
for(int j = 1; j < MAXN; j++) {
dis[j] = INF;
}
dis[st] = 0;

while (!queue.isEmpty()) {
int qstart = queue.getFirst();
queue.removeFirst();
inq[qstart] = false;

for (int e = first[qstart]; e != -1; e = next[e]) {
if (dis[venpoint[e]] > dis[qstart] + wlen[e]) {
dis[venpoint[e]] = dis[qstart] + wlen[e];
if (!inq[venpoint[e]]) {
queue.addLast(venpoint[e]);
inq[venpoint[e]] = true;
}
}
}
}
System.out.println(dis[ed]);
}
}


C++解法:http://www.mamicode.com/info-detail-400547.html

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;

const int MAX = 100005;
int dis[MAX], vis[MAX] ,head[MAX];
int n, m, s, t, cnt;

struct edge
{
int v, w, next;
}edge[1000005];

void addedge(int u, int v, int w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}

void spfa(int s)
{
int pos, v;
queue<int> q;
q.push(s);
dis[s] = 0; vis[s] = 1;
while(!q.empty())
{
pos = q.front(); q.pop();
vis[pos] = 0;
for(int i = head[pos]; i != -1; i = edge[i].next)
{
v = edge[i].v;
if(dis[pos] + edge[i].w < dis[v])
{
dis[v] = dis[pos] + edge[i].w;
if(!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
}

int main()
{
while(scanf("%d %d %d %d", &n, &m, &s, &t) != EOF)
{
cnt = 0;
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
memset(dis, 0x37, sizeof(dis));
while(m--)
{
int x, y, w;
scanf("%d %d %d", &x, &y, &w);
addedge(x, y, w);
addedge(y, x, w);
}
spfa(s);
printf("%d\n", dis[t]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hihocoder 算法