POJ 3255-Roadblocks(A*+spfa)

Roadblocks
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11063 Accepted: 3921

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


注意题目给的是无向边。


AC代码:

#include<iostream>
#include<functional>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
#define QWQ ios::sync_with_stdio(0)
#define inf 0x3f3f3f3f
typedef unsigned long long LL;
typedef  long long ll;

const int T = 500000+50;
const int mod = 1000000007;

int n,m,c;
int head[T],tail[T];

struct node
{
	int v,next,w;
}e1[T],e2[T];

struct line
{
	int f,v,g;
	bool operator<(const line& b)const{
		return f>b.f||(f==b.f&&g>b.g);
	}
};

void Init()
{
	c = 0;
	fill(head,head+T,-1);
	fill(tail,tail+T,-1);
}

void add(int x,int y,int w)
{
	e1[c].v = y,e1[c].next = head[x];
	e1[c].w = w;
	head[x] = c;

	e2[c].v = x,e2[c].next = tail[y];
	e2[c].w = w;
	tail[y] = c++;

	e1[c].v = x,e1[c].next = head[y];
	e1[c].w = w;
	head[y] = c;

	e2[c].v = y,e2[c].next = tail[x];
	e2[c].w = w;
	tail[x] = c++;
}

int dis[T];
bool vis[T];

void spfa()//要从终点扩展
{
	fill(dis,dis+T,inf);
	fill(vis,vis+T,false);
	dis[n] = 0;
	queue<int> Q;
	Q.push(n);
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		vis[u] = false;
		for(int i = tail[u];~i;i=e2[i].next){
			int v = e2[i].v;
			int w = e2[i].w;
			if(dis[v]>dis[u]+w){
				dis[v] = dis[u] + w;
				if(!vis[v]){
					Q.push(v);
					vis[v] = true;
				}
			}
		}
	}
}

int Astar()
{
	priority_queue<line> A;
	line t,tt;
	t.v = 1,t.g = 0,t.f = t.g + dis[1];
	if(dis[1]==inf)return -1;
	A.push(t);
	int cnt = 0;
	while(!A.empty())
	{
		tt = A.top();A.pop();
		if(tt.v==n){
			cnt++;
			if(cnt==2){
				return tt.g;
			}
		}
		for(int i=head[tt.v];~i;i=e1[i].next){
			t.v = e1[i].v;
			t.g = tt.g + e1[i].w;//起点值
			t.f = t.g + dis[t.v];//终点值
			A.push(t);
		}
	}
	return -1;
}

int main()
{

#ifdef zsc
    freopen("input.txt","r",stdin);
#endif

    int i,j;
	int u,v,w;
   while(~scanf("%d%d",&n,&m))
   {
	   Init();
	   for(i=1;i<=m;++i){
		   scanf("%d%d%d",&u,&v,&w);
		   add(u,v,w);
	   }
	   
	   spfa();
	   printf("%d\n",Astar());
   }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值