POJ3255 - Roadblocks - 次短路(spfa+LLL优化)

1.题目描述:

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14158 Accepted: 4976

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)

Source

2.题意概述:

给你一个图,小明比较爱玩,想多欣赏路上风景,因此选择次短路出发,问你图的次短路长度

3.解题思路:

暴力成就一切,分别对起点和终点做一遍最短路(记录起点最短路为dis1,终点的最短路为dis2),然后枚举每一条边,把u到v改写成dis1[u] + dis2[v] + distance_between_u_v;

因为最短路可能有多条,因此最好用个set维护一下,或者在最短路基础上另外更新。

4.AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 5111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
	int v, w;
	node(int a, int b) { v = a; w = b; }
	friend bool operator< (node a, node b)
	{
		return a.w > b.w;
	}
};
vector<node> mp[N];
set<int> s;
int dis1[N], dis2[N];
bool vis[N];
void spfa(int sta, int n, int dis[])
{
	memset(vis, 0, sizeof(vis));
	fill(dis, dis + n + 1, INF);
	priority_queue<node> q;
	vis[sta] = 1;
	dis[sta] = 0;
	q.push(node(sta, 0));
	while (!q.empty())
	{
		int u = q.top().v;
		q.pop();
		vis[u] = 0;
		int sz = mp[u].size();
		for (int i = 0; i < sz; i++)
		{
			int v = mp[u][i].v;
			int val = mp[u][i].w;
			if (dis[v] > dis[u] + val)
			{
				dis[v] = dis[u] + val;
				if (!vis[v])
				{
					vis[v] = 1;
					q.push(node(v, dis[v]));
				}
			}
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n, r;
	while (~scanf("%d%d", &n, &r))
	{
		for (int i = 1; i <= n; i++)
			mp[i].clear();
		for (int i = 0; i < r; i++)
		{
			int a, b, d;
			scanf("%d%d%d", &a, &b, &d);
			mp[a].push_back(node(b, d));
			mp[b].push_back(node(a, d));
		}
		spfa(1, n, dis1);
		spfa(n, n, dis2);
		s.clear();
		for (int i = 1; i <= n; i++)
		{
			int sz = mp[i].size();
			for (int j = 0; j < sz; j++)
			{
				int to = mp[i][j].v;
				int val = mp[i][j].w;
				s.insert(dis1[i] + dis2[to] + val);
			}
		}
		set<int>::iterator it = s.begin();
		printf("%d\n", *(++it));
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值