题目链接:http://poj.org/problem?id=3013
本题是最短路问题。
但是较特殊的一点是边少点多。构建临界矩阵不合理,因为是稀疏图,且点较多,会爆栈。
所以,正确的做法使用邻接表,邻接表又分为基于点的邻接表和基于边的,对于本题而言,我们用基于边的链式前向星较好。
链式前向星也就是我们经常说的池子法。
另外,对于最短路的处理,我们用SPFA较好。
所以,综合,本题练习基于前向星的SPFA。
其实,SPFA处理最短路问题,不过是队列优化的Bellman-ford而已,具体参考刘汝佳《算法竞赛入门经典》205页。
代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define ll __int64
#define INF ((ll)1<<63)-1
#define Maxn 50005
struct Edge
{
int s;
int t