Roadblocks--poj3255(次短路)

题目链接

求次短路的问题;

dist[i][0]和dist[i][1]表示从起点1到i的距离和从起点n到i的距离;

次短路要比最短路大但小于其他路;

每条路1--n的距离都可以用dist[i][0] + dist[j][1] + G[j].w表示;、、具体的可以仔细想想;
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define N 5060

struct Edge
{
    int e,w;
    Edge(int x=0, int y=0):e(x),w(y){}
};

vector<Edge> G[N];
int dist[N][2], n, m, vis[N];

void spfa(int s, int k)
{
    queue<Edge>Q;
    Edge p, q;
    dist[s][k] = 0;
    Q.push(Edge(s, 0));
    while(Q.size())
    {
        p=Q.front(); Q.pop();
        vis[p.e] = 1;
        int len = G[p.e].size();
        for(int i=0; i<len; i++)
        {
            q = G[p.e][i];
            if(dist[q.e][k] > dist[p.e][k] + q.w)
            {
                dist[q.e][k] = dist[p.e][k] + q.w;
                if(vis[q.e] == 0)
                {
                    Q.push(q);
                    vis[q.e] = 1;
                }
            }
        }
    }

}

int main()
{
    int a, b, c;
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        for(int i=0;i<=n;i++)
        {
            G[i].clear();
            dist[i][0]= dist[i][1]=INF;

        }
        for(int i=0; i<m;i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            G[a].push_back(Edge(b, c));
            G[b].push_back(Edge(a, c));
        }
        spfa(1, 0);
        spfa(n, 1);

        int ans = INF;
        for(int i=1; i<=n; i++)
        {
            int len = G[i].size();
            for(int j=0; j<len; j++)
            {
                int W=G[i][j].w + dist[i][0] +dist[G[i][j].e][1];
                if(ans>W && W>dist[n][0])
                {
                    ans=W;
                }
            }
        }
        printf("%d\n", ans);

    }
}
时间: 2024-09-29 17:46:33

Roadblocks--poj3255(次短路)的相关文章

POJ3255 Roadblocks 【次短路】

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7760   Accepted: 2848 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 (次短路模板)

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【次短路】

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

POJ3255(次短路)

题目链接:点击打开链接 解题思路: 按照Dijkstra思想做的次短路,第一次用邻接表,注意题中是双向边并且节点的下标要分别-1. 完整代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <set> #include <vector> #include <

poj3255 次短路的长度

这道问题是求1-N的次短路的长度,我们直接在dist[maxn][2]上加1维更新即可, 代码如下: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; const int maxn = 5000 + 10; int N, R; struct edge { int v, c

【POJ】3255 Roadblocks(次短路+spfa)

http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立.有可能在前边两个if改了后还有更优的次短路. 所以,,wikioi那题太水,让我水过了.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <

小结:最短路

概要: 最短路是个神奇的东西,通过三角不等式,我们可以拓展出很多最短路的延伸.而求最短路的算法一般我用三种,dijkstra.spfa.floyd,第一个用于点少边多的,第一个用于点多边少的,第三个是多源最短路. 应用: 差分约束系统.一般约束条件.最短路等. 技巧及注意: 差分约束:根据三角不等式d(v)<=d(u)+w(u, v),我们通过移项,还可以得到d(v)+w(u, v)<=d(u),而这样就足以解决一些不等式约束集了,即差分约束(在一些情况下,可以考虑最长路的三角不等式).例如[

7、8月刷题总结

准备开学了囧,7.8月刷题记录,以后好来复习,并且还要好好总结! 数据结构: splay: [BZOJ]1503: [NOI2004]郁闷的出纳员(Splay) [BZOJ]1269: [AHOI2006]文本编辑器editor(Splay) [BZOJ]1507: [NOI2003]Editor(Splay) treap: [BZOJ]1862: [Zjoi2006]GameZ游戏排名系统 & 1056: [HAOI2008]排名系统(treap+非常小心) [BZOJ]3224: Tyvj

POJ3255:Roadblocks(次短路 SPFA+A星)

给出1-N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了 题目: I - RoadblocksTime 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 o

POJ3255 Roadblocks [Dijkstra,次短路]

题目传送门 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