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 INF 0x3f3f3f3f
struct edge
{
    int to,cost;
 };
typedef pair<int,int> P;//first是最短距离,second是顶点的编号
 int n,r;
 vector<edge>G[100005];
 int d[100005];
 int dist[100005],dist2[100005];//最短路和次短路 

 void dijkstra()
 {
     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 q=que.top();    que.pop();
         int v=q.second,d=q.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()
 {
     int a,b,c;
     while(cin>>n>>r)
     {
         for(int i=0;i<n;i++)
             G[i].clear();
         for(int i=0;i<r;i++)
         {
             scanf("%d%d%d",&a,&b,&c);
             a--;
             b--;
             G[a].push_back(edge{b,c});//存放边 //在结构体重存数用花括号
             G[b].push_back(edge{a,c});//无向边
         }
         dijkstra();
     }
     return 0;
 }
时间: 2024-11-03 03:46:34

poj3255的相关文章

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

cogs 315. [POJ3255] 地砖RoadBlocks

315. [POJ3255] 地砖RoadBlocks ★★★   输入文件:block.in   输出文件:block.out   简单对比时间限制:1 s   内存限制:128 MB 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 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: 13594   Accepted: 4783 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 【次短路】

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

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

POJ——T 3255 Roadblocks|| COGS——T 315. [POJ3255] 地砖RoadBlocks || 洛谷—— P2865 [USACO06NOV]路障Roadblocks

http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15680   Accepted: 5510 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

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