POJ3255(次短路)

题目链接:点击打开链接

解题思路:

按照Dijkstra思想做的次短路,第一次用邻接表,注意题中是双向边并且节点的下标要分别-1.

完整代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <climits>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 100001;

typedef pair<int , int> P;

struct edge
{
    int to , cost;
};

int INF = 99999999;
int n , r;
vector<edge> g[maxn];
int dist[maxn];
int dist2[maxn];

void 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 p = que.top();
        que.pop();
        int v = p.second , d = p.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));
            }
        }
    }
    cout << dist2[n-1] << endl;
}

int main()
{

    #ifdef DoubleQ
    freopen("in.txt" , "r" , stdin);
    #endif
    while(cin >> n >> r)
    {
        int a , b , d;
        for(int i = 0 ; i < r ; i ++)
        {
            cin >> a >> b >> d;
            a --;
            b --;
            struct edge temp;
            temp.to = b;
            temp.cost = d;
            g[a].push_back(temp);

            struct edge temp2;
            temp2.to = a;
            temp2.cost = d;
            g[b].push_back(temp2);
        }

        solve();
    }
    return 0;
}

更多精彩请访问:点击打开链接

时间: 2024-11-20 19:07:33

POJ3255(次短路)的相关文章

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

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

poj3255 Roadblocks 次短路

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

POJ3255 Roadblocks 次短路Dijkstra做法

有段时间没做题了,这几天一直在寻找感觉,尽量多看书,这题目就是n个地方,编号从1到n,然后有r条路,问你从1号到达n号地方的次短路长度为多少,同一条边可以重复走,直接在dijkstra算法里同时记录一个次短路就可以了,但是一直WA,后来去看了讨论面板,那里有人给了测试数据,我不知道那数据的对错,但是干扰了我很久,也许那些数据实在题目案例之外的吧,对我的程序没有任何影响,我只是太久没错 一时 忘了双向边了,一开始只建立了单向边~代码写的也比较冗长~ int n,r; typedef struct

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

poj3255

poj3255 题意:输入u,r表示有n个点,r条无向边 输出1到n的次短路. 题解:单源最短路,使用优先队列优化.模板题. #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<map> #include<algorithm> using namespace std; #define

最短路与次短路

1.poj3255  求次短路 思路:遍历每条边<u, v>, 看begin[u] + dis<u, v> + end[v] 是否为次短路 //求次短路 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <queu

【NOIP复习】最短路总结

[模板] 1 /*堆优化Dijkstra*/ 2 3 void dijkstra() 4 { 5 priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > que;//定义大顶堆 6 for (int i=1;i<=n;i++) vis[i]=0,dis[i]=INF; 7 dis[1]=0; 8 que.push(make_pair&l