POJ3255Roadblocks[次短路]

Roadblocks

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12697   Accepted: 4491

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

USACO 2006 November Gold


d[u][0]和d[u][1]分别最短路和次短路

更新时类似DP求树的直径

spfa:有一个点的最短路或次短路更新了,把这个店加进去

dijkstra:hn结构体中多一个p,0最短路,1次短路

//spfa 32MS NO.1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=5005,M=100005,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x;
}
int n,m,u,v,w;
struct edge{
    int v,w,ne;
}e[M<<1];
int h[N],cnt=0;
inline void ins(int u,int v,int w){
    cnt++;
    e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][2],inq[N],q[N],head=1,tail=0;
void spfa(){
    for(int i=1;i<=n;i++){d[i][0]=d[i][1]=INF;}
    d[1][0]=0; inq[1]=1; q[++tail]=1;
    while(head<=tail){
        int u=q[head++];//printf("u %d\n",u);
        inq[u]=0;
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(d[v][0]>d[u][0]+w){
                d[v][1]=d[v][0];
                d[v][0]=d[u][0]+w;
                if(!inq[v]){inq[v]=1;q[++tail]=v;}
            }else if(d[v][1]>d[u][0]+w&&d[v][0]<d[u][0]+w){
                d[v][1]=d[u][0]+w;
                if(!inq[v]){inq[v]=1;q[++tail]=v;}
            }
            if(d[v][1]>d[u][1]+w){
                d[v][1]=d[u][1]+w;
                if(!inq[v]){inq[v]=1;q[++tail]=v;}
            }
        }
    }
}
int main(int argc, const char * argv[]) {
    n=read();m=read();
    for(int i=1;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
    spfa();
    printf("%d",d[n][1]);
    return 0;
}
//dijkstra 63MS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int N=5005,M=100005,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x;
}
int n,m,u,v,w;
struct edge{
    int v,w,ne;
}e[M<<1];
int h[N],cnt=0;
inline void ins(int u,int v,int w){
    cnt++;
    e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
    cnt++;
    e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][2],vis[N][2];
struct hn{
    int u,d,p;
    hn(int a=0,int b=0,int c=0):u(a),d(b),p(c){}
    bool operator < (const hn &rhs)const{return d>rhs.d;}
};
priority_queue<hn> q;
void dijkstra(){
    for(int i=1;i<=n;i++) {d[i][0]=d[i][1]=INF;}
    q.push(hn(1,0,0));
    d[1][0]=0;
    while(!q.empty()){
        hn now=q.top();q.pop();
        int u=now.u,p=now.p;
        if(vis[u][p]) continue;
        vis[u][p]=1;
        for(int i=h[u];i;i=e[i].ne){
            int v=e[i].v,w=e[i].w;
            if(d[v][0]>d[u][p]+w){
                d[v][1]=d[v][0];
                d[v][0]=d[u][p]+w;
                q.push(hn(v,d[v][0],0));
                q.push(hn(v,d[v][1],1));
            }else if(d[v][1]>d[u][p]+w){
                d[v][1]=d[u][p]+w;
                 q.push(hn(v,d[v][1],1));
            }
        }
    }
}
int main(int argc, const char * argv[]) {
    n=read();m=read();
    for(int i=1;i<=m;i++){u=read();v=read();w=read();ins(u,v,w);}
    dijkstra();
    printf("%d",d[n][1]);
    return 0;
}
时间: 2024-08-06 21:06:03

POJ3255Roadblocks[次短路]的相关文章

POJ3255-Roadblocks(最短路)

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-shorte

POJ-3255-Roadblocks(次短路的另一种求法)

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 th

hdu3461Marriage Match IV 最短路+最大流

//给一个图.给定起点和终点,仅仅能走图上的最短路 //问最多有多少种走的方法.每条路仅仅能走一次 //仅仅要将在最短路上的全部边的权值改为1.求一个最大流即可 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> using namespace std ; const int inf = 0x3f3f3f3f ; const

UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的T-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据. 每组数据第一行是两个整数NN ,MM (N≤100N≤100 ,M≤10000M≤1000

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

//转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路 //Time:16Ms Memory:208K #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define MAX 105 int lim, n; int p[M

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

hdu4725 拆点+最短路

题意:有 n 个点,每个点有它所在的层数,最多有 n 层,相邻两层之间的点可以互相到达,消耗 c (但同一层并不能直接到达),然后还有一些额外的路径,可以在两点间互相到达,并且消耗一定费用.问 1 点到 n 点的最小花费 将每一层拆成两个点,分别为进入层和出发层,然后相邻层的出发层可以指向进入层,花费 c,每个点可以到达其出发层,而进入层可以到达该点,花费 0 ,最后建立其余双向边,最短路 1 #include<stdio.h> 2 #include<string.h> 3 #in

hdu3416 最短路+最大流

题意:有 n 点 m 边,有出发点 A 到达点 B ,只允许走原图中的最短路,但每条边只允许被走一次,问最多能找出多少条边不重复的最短路 一开始做到的时候瞎做了一发最短路,WA了之后也知道显然不对,就放着了,后来打了今年的多校,再做到的时候发现和多校第一场的1007一样的……最短路+网络流就行了,只不过第一次做这个的时候我还不知道网络流是啥,不会做也正常啦. 首先对于原图跑一遍最短路求出每个点距离 A 点的最短路,然后对于每一条边,如果它的权值等于它连接的两点的最短路的差值的时候,就说明这条路是