POJ 3255 Roadblocks (次短路模板)

Roadblocks

http://poj.org/problem?id=3255

Time Limit: 2000MS   Memory Limit: 65536K
     

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 intersectionN.

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)

题意:求严格次短路

次短路一定是由最短路上出去经过其他的边在回到最短路上

所以先求出每个点到1的最短路dis1,每个点到n的最短路dis2

枚举每一条边

那么经过这条边u-->v的最短路为dis1[u]+w+dis2[v]

如果它大于最短路,更新答案

#include<queue>
#include<cstdio>
#include<cstring>
#define N 5001
#define M 100001
using namespace std;
int front[N],to[M<<1],nxt[M<<1],val[M<<1],from[M<<1],tot;
int dis1[N],dis2[N];
bool vis[N];
queue<int>q;
void add(int u,int v,int w)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w; from[tot]=u;
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w; from[tot]=v;
}
void spfa(int s,int *dis)
{
    //memset(dis,63,sizeof(dis));
    memset(vis,false,sizeof(vis));
    q.push(s);
    vis[s]=true;
    dis[s]=0;
    int now;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        vis[now]=false;
        for(int i=front[now];i;i=nxt[i])
            if(dis[to[i]]>dis[now]+val[i])
            {
                dis[to[i]]=dis[now]+val[i];
                if(!vis[to[i]])
                {
                    vis[to[i]]=true;
                    q.push(to[i]);
                }
            }
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int u,v,w;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
    }
    memset(dis1,63,sizeof(dis1));
    memset(dis2,63,sizeof(dis2));
    spfa(1,dis1);
    spfa(n,dis2);
    int minn=dis1[n],tmp,ans=0x7fffffff;
    for(int i=1;i<=tot;i++)
    {
        u=from[i]; v=to[i];
        tmp=dis1[u]+val[i]+dis2[v];
        if(tmp>minn && tmp<ans) ans=tmp;
    }
    printf("%d",ans);
}
时间: 2024-10-07 07:02:22

POJ 3255 Roadblocks (次短路模板)的相关文章

POJ 3255 Roadblocks (次短路问题)

解法有很多奇葩的地方,比如可以到达终点再跳回去再跳回来(比如有两个点)....反正就是不能有最短路,不过没关系,算法都能给出正确结果 思想:和求最短路上的点套路一样,spfa先正着求一次,再反着求一次最短路,然后枚举每条边<i,j>找dist_zheng[i] + len<i,j> + dist_fan[j]的第二小值即可!注意不能用邻接矩阵,那样会MLE,应该用邻接表 /* poj 3255 3808K 266MS */ #include<cstdio> #inclu

POJ - 3255 Roadblocks (次短路)

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

poj 3255 Roadblocks【次短路】

题目:poj 3255 Roadblocks 题意:给出一个无向图,然后求1到n点的次短路 分析:两种做法,第一种,Astat+最短路求k短路的方法. 第二种是比较暴力的方法. 先求1点到所有点的最短路dis1 然后求n点到所有点的最短路dis2 然后枚举所有边,则次短路为dis1[from] + dis2[to] + w[i]中大于最短路的最短的. AC代码: #include <cstdio> #include <string> #include <cstring>

POJ 2449Remmarguts&#39; Date K短路模板 A*+SPFA

太水了我不想说了,模板在这里 14312K 313MS 1 #include<queue> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int v[100010],v2[100010],c[100010],c2[100010],s,t,k,duin; 7 int n,m,point[1010],next[100010],cnt=0,

POJ 3255 Roadblocks

Roadblocks Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 325564-bit integer IO format: %lld      Java class name: Main Bessie has moved to a small farm and sometimes enjoys returning to visit one of her bes

poj - 3225 Roadblocks(次短路)

http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标号为1到N,问1号路口到N号路口的次短路长度是多少?次短路是 比最短路长度长的次短的路径.同一条边可以经过多次. 到某个顶点v的次短路要么帅到其他顶点u的最短路在加上u-v的边,要么是到u的次短路再加上u-v的边, 因此所需要求的就是到所有顶点的最短路和次短路,因此,对于每个顶点,我们记录的不仅仅是

POJ 3255 Roadblocks (次短路径 + Dijkstra算法)

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7982   Accepted: 2921 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 q

POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 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

POJ 3255 Roadblocks Dijkstra 算法变形

#include <cstdio> #include <iostream> #include <queue> using namespace std; const int INF = 1000000; const int maxn = 5005; struct edge{ int y,w; edge(int cy,int ww){ y = cy; w = ww; } }; vector<edge> vec[maxn]; int n,m; struct num