POJ 3255

题意: 次短路,

裸的板子;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 1e5 +131;
const int INF = 0x7fffffff;
struct Edge{
    int to, cost;
    Edge(int a, int b) : to(a), cost(b) {}
};

vector<Edge> G[maxn];
void AddEdge(int from, int to, int cost) {
    G[from].push_back(Edge(to, cost));
    G[to].push_back(Edge(from, cost));
}

typedef pair<int, int> P;
int N, R;
int Dist[maxn], Dist2[maxn];

int Solve()
{
    priority_queue<P, vector<P>, greater<P> > que;
    fill(Dist, Dist + N, INF);
    fill(Dist2, Dist2 + N, INF);
    Dist[0] = 0;
    que.push(P(0,0));

    while(!que.empty())
    {
        P tmp = que.top(); que.pop();
        int v = tmp.second, d = tmp.first;
        if(Dist2[v] < d) continue;
        for(int i = 0; i < G[v].size(); ++i)
        {
            Edge &e = G[v][i];
            int d2 = d + e.cost;
            if(Dist[e.to] > d2) {
                swap(Dist[e.to], d2);
                que.push(P(Dist[e.to], e.to));
            }
            if(Dist2[e.to] > d2 && Dist[e.to] < d2)
            {
                Dist2[e.to] = d2;
                que.push(P(Dist2[e.to], e.to));
            }
        }
    }
    return Dist2[N-1];
}

int main()
{
    while(scanf("%d%d", &N, &R)!=EOF)
    {
        if(N == 0 && R == 0) break;
        for(int i = 0; i <= N; ++i) G[i].clear();
        for(int i = 0; i < R; ++i)
        {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            AddEdge(u-1,v-1,c);
        }
        printf("%d\n",Solve());
    }
    return 0;
}
时间: 2024-10-05 08:31:28

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

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

(次短路) 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 q