首页 > 数据库技术 > 详细

POJ - 3255 Roadblocks (次短路)

时间:2015-04-11 14:50:34      阅读:238      评论:0      收藏:0      [点我收藏+]
POJ - 3255
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: A, B, 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

USACO 2006 November Gold

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
#include<set>
#include<stack>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
    char c;
    while((c=getchar())<=32)if(c==EOF)return -1;
    bool ok=false;
    if(c=='-')ok=true,c=getchar();
    for(x=0; c>32; c=getchar())
        x=x*10+c-'0';
    if(ok)x=-x;
    return 1;
}
template<class T> inline T read_(T&x,T&y)
{
    return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
    return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
    write(x);
    putchar('\n');
}
//-------ZCC IO template------
const int maxn=1000001;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long  LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007

int V,E;
struct edge
{
    int to,cost;
    edge(int a,int b){to=a;cost=b;}
};
vector<edge> G[maxn];
int d[maxn];
int d1[maxn];

void solve(int s)
{
    priority_queue<P,vector<P>,greater<P> > q;
    For(i,0,V+1)
    {
        d[i]=d1[i]=inf;
    }
    q.push(P(0,s));

    while(!q.empty())
    {
        P tmp=q.top();q.pop();
        int v=tmp.second;int dis=tmp.first;
        if(d1[v]<dis)continue;
        int N=G[v].size();
        for(int i=0;i<N;i++)
        {
            edge e=G[v][i];
            int d2=dis+e.cost;
            if(d[e.to]>d2)
            {
                swap(d[e.to],d2);
                //d[e.to]=d2;
                q.push(P(d[e.to],e.to));
            }
            if(d1[e.to]>d2&&d2>d[e.to])
            {
                d1[e.to]=d2;
                q.push(P(d1[e.to],e.to));
            }
        }
    }
    writeln(d1[V]);
}
int main()
{
  //#ifndef ONLINE_JUDGE
   //freopen("in.txt","r",stdin);
 // #endif // ONLINE_JUDGE
    int n,m,i,j,k,t;
    while(read_(V,E))
    {
        for(int i=0;i<E;i++)
        {
            int a,b,c;
            read__(a,b,c);
            G[a].push_back(edge(b,c));
            G[b].push_back(edge(a,c));
        }
        solve(1);
        For(i,0,V)G[i].clear();
    }
    return 0;
}

POJ - 3255 Roadblocks (次短路)

原文:http://blog.csdn.net/u013167299/article/details/44995127

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!