(次短路) poj 3255

Roadblocks

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8706   Accepted: 3145

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)

题意:

求次短路。。

思路

dist1 ,dist2分别为 1,n出发的最短路,然后 枚举 u,v dist[u]+dist[v]+w[u][v]中求出次小就好

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#define INF 100000000
using namespace std;
vector<int> e[5001],w[5001];
int dist1[5001],dist2[5001];
bool vis[5001];
int n,m;
void spfa(int st,int dist[])
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    dist[st]=0;
    vis[st]=1;
    q.push(st);
    while(!q.empty())
    {
        int x;
        x=q.front(),q.pop();
        vis[x]=0;
        for(int i=0;i<e[x].size();i++)
        {
            int v=e[x][i];
            if(dist[x]+w[x][i]<dist[v])
            {
                dist[v]=dist[x]+w[x][i];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        e[x].push_back(y);
        e[y].push_back(x);
        w[x].push_back(z);
        w[y].push_back(z);
    }
    spfa(1,dist1);
    spfa(n,dist2);
    int ans1,ans2;
    ans1=ans2=INF;
    for(int u=1;u<=n;u++)
    {
        for(int j=0;j<e[u].size();j++)
        {
            int v=e[u][j];
            if(dist1[u]+dist2[v]+w[u][j]<ans1)
            {
                ans2=ans1;
                ans1=dist1[u]+dist2[v]+w[u][j];
            }
            else if(dist1[u]+dist2[v]+w[u][j]<ans2&&dist1[u]+dist2[v]+w[u][j]!=ans1)
            {
                ans2=dist1[u]+dist2[v]+w[u][j];
            }
        }
    }
    printf("%d\n",ans2);
    return 0;
}

  

时间: 2024-10-21 21:26:52

(次短路) poj 3255的相关文章

POJ 3255 &amp;&amp; HDU 1688 &amp;&amp; HDU 3191 次短路问题

POJ 3255 Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7627   Accepted: 2798 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 h

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

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 题解

题目大意 给你一个 \(n\) 个点,\(m\) 条边的无向图,求出这个无向图中从1到n的次短路.其中\(n \le 5000\),\(m \le 100000\). 题目传送门 POJ 3255 思路 其实求次长路是很简单的一个问题,但是网上却有很多算法都过于复杂了.首先我们先求出从1到每个结点的最短路长度(用Dijkstra或者是SPFA都可以),存入数组 \(dis1\) 中,然后再求出从结点 \(n\) 到任意结点的最短路的长度,存入数组 \(dis2\) 中. 然后我们枚举这个图中的所

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 quick

Roadblocks POJ 3255(次短路)

原题 题目链接 题目分析 给无向图,求次短路.相对于第k短路而言次短路还是好求的,只需要在跑dijkstra的过程中顺便记录次短路就行了. 代码 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <utility> 6 #include <ctime> 7 #include <cmath

POJ 3255(dijkstra优化,次最短路)

题意:求 1 - n 的次最短路 分析: 先来谈谈Dijkstra的优化.对于每次寻找到当前为访问过的点中距离最短的那一个,运用优先队列进行优化,避免全部扫描,每更新一个点的最短距离就加入优先队列.有人会问,一个点如果已经处理完成了,那它还留在队列中怎么办?我们放入队列时将一个点那时的顶点编号和最短距离进行打包,如果取出该点时,它当前的最短距离小于该点标记的最短距离,说明该点已经取到最短距离,不进行操作.或者直接用一个vis数组来记录某一个点是否已经取到最短距离:其次的优化是用邻接表存储与每一个

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