【图论】POJ-3255 次短路径

一、题目

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

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)

二、思路&心得

  • 定义权值的最大值时,尽可能大,综合题目的条件,比如这题的MAX_D就应该大于两倍的D最大值
  • 注意题目条件:比如这题存在自环边、双向边,同一条道路可以经过
  • 图论中,注意根据题目图的特点选择对应的算法,比如这题为稀疏图,应选择邻接表进行表示图

三、代码

#include<cstdio>
#include<vector>
#include<algorithm>
#define MAX_SIZE 5005
#define MAX_D 10005
using namespace std;

struct edge {
	int to, cost;
};

vector<edge> G[MAX_SIZE];

int N, R;
int dist[MAX_SIZE];
int dist2[MAX_SIZE];
int visit[MAX_SIZE];
int visit2[MAX_SIZE];

void Dijkstra(int s) {
	dist[s] = 0;
	while (true) {
		int min_D = MAX_D;
		int index;
		int d, flag = 0;
		for (int j = 1; j <= N; j ++) {
			if (!visit[j] && dist[j] < min_D) {
				min_D = dist[j];
				index = j;
				flag = 1;
			} else if (!visit2[j] && dist2[j] < min_D) {
				min_D = dist[j];
				index = j;
				flag = 2;
			}
		}
		
		if (!flag) break;
		else if (flag == 1) {
			visit[index] = 1;
			d = dist[index];
		}
		else if (flag == 2) {
			visit2[index] = 1;
			d = dist2[index];
		}
		
		for (int k = 0; k < G[index].size(); k ++) {
			edge e = G[index][k];
			int temp = e.cost + d;
			if (!visit[e.to] && temp < dist[e.to]) {
				swap(dist[e.to], temp);
			}
			if (dist[e.to] < temp && temp < dist2[e.to]) {
				dist2[e.to] = temp;
				visit2[e.to] = 0;
			}
		}
	}
	printf("%d\n", dist2[N]);
}

void solve() {
	for (int i = 1; i <= N; i ++) {
		visit[i] = 0;
		visit2[i] = 1;	
		dist[i] = MAX_D;
		dist2[i] = MAX_D;
		G[i].clear();
	}
	int from;
	edge e;
	for (int i = 1; i <= R; i ++) {
		scanf("%d %d %d", &from, &e.to, &e.cost);
		G[from].push_back(e);
		swap(from, e.to);
		G[from].push_back(e);
	}
	Dijkstra(1);	
}

int main() {
	while (~scanf("%d %d", &N, &R)) { 
		solve();	
	}
	return 0; 
}
posted @ 2017-08-03 17:44  天涯惟笑  阅读(195)  评论(0编辑  收藏  举报