POJ - 3255 Roadblocks(最短路Dijkstra算法求次最短路)

Roadblocks
Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤ N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersection N.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Line 1: Two space-separated integers:  N and  R
Lines 2..  R+1: Each line contains three space-separated integers:  AB, and  D that describe a road that connects intersections  A and  B and has length  D (1 ≤  D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and node  N

Sample Input

4 4
1 2 100
2 4 200
2 3 250
3 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)


题意:

某街道共有R条道路、N个路口。道路可以双向通行。问1号路口到N号路口的次短路长度是多少?同一条边可以经过多次。

分析:

   Dijkstra 算法的思路是依次确定尚未确定的顶点中距离最小的顶点,利用这个思路,可以看到:到某个顶点v的次短路要么是其他某个顶点u的最短路再加上u->v这条边(且加上后要求比最短路长),要么就是u的次短路再加上u->v的边。于是我们用Dijkstra的思路可以求出所有顶点的最短路和次短路。

由于边是可以重复经过的,对下面的样例,输出是140,与常识不同。

输入:

4 4

1 2 100

2 4 200

2 3 10

3 4 10

输出:

140


#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int mnx = 5000 + 100;
const int mxe = 100000 + 1000;
const int INF = 0X3f;

int dis[mnx];
int dis2[mnx];
int vis[mnx];
vectorg[mnx],e[mnx];
int n, m;

struct point
{
	int num;
	int dis;
	bool operator < (const point & b)const
	{
		return dis>b.dis;
	}
	point(int d, int n)
	{
		num = n;
		dis = d;
	}
};

void add(int v, int u,int c)
{
	g[v].push_back(u);
	e[v].push_back(c);
}

void dijkstra(int s)
{
	memset(dis, INF, sizeof(dis));
	memset(dis2, INF, sizeof(dis2));

	priority_queueque;
	
	dis[s] = 0;
	que.push(point(0, 1));

	while (!que.empty())
	{
		point p = que.top(); que.pop();
		int v = p.num;
		int d = p.dis;
		if (dis2[v] < d) continue;
		for (int i = 0; i < g[v].size(); i++)
		{
			int c = e[v][i];
			int u = g[v][i];
			int d2 = d + c;
			if (dis[u] > d2)
			{
				swap(dis[u], d2);
				que.push(point(dis[u], u));
			}
			if (dis2[u] > d2 && dis[u] < d2)
			{
				dis2[u] = d2;
				que.push(point(dis2[u], u));
			}	
		}
	}
}

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		int u, v, c;
		for (int i = 0; i < m; i++)
		{
			scanf("%d%d%d", &u, &v, &c);
			add(u, v, c);
			add(v, u, c);
		}

		dijkstra(1);

		printf("%d\n", dis2[n]);
	}
}


你可能感兴趣的:(图论)