zoukankan      html  css  js  c++  java
  • [POJ3255] 地砖RoadBlocks

    [POJ3255] 地砖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 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 intersectionN.

    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)

    Source

     
    求次短路 
    可以用A*
    这里我用的dijs+堆优化
    题目有点坑 
    这个可以来回跑。。 从1->3->1->...->n;的路径合法
    SPFA好像没法重复跑 
    所以我WA了又WA 就改了dijs
     
     1 #include <algorithm>
     2 #include <iostream>
     3 #include <ctype.h>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <queue>
     7 
     8 using namespace std;
     9 
    10 const int MAXN=5010;
    11 const int INF=1e9;
    12 
    13 int n,m;
    14 
    15 int dis[MAXN],dis2[MAXN];
    16 
    17 typedef pair<int,int> P;
    18 
    19 struct edge {
    20     int to;
    21     int val;
    22 };
    23 vector<edge> G[MAXN];
    24 
    25 inline void read(int&x) {
    26     int f=1;register char c=getchar();
    27     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
    28     for(;isdigit(c);x=x*10+c-48,c=getchar());
    29     x=x*f;
    30 }
    31 
    32 inline void SPFA() {
    33     priority_queue<P,vector<P>,greater<P> >Q;
    34     for(int i=0;i<=n;++i) dis[i]=dis2[i]=INF;
    35     Q.push(P(1,0));
    36     dis[1]=0;
    37     while(!Q.empty()) {
    38         P u=Q.top();
    39         Q.pop();
    40         int v=u.first,V=u.second;
    41         if(dis2[v]<V) continue;
    42         for(int i=0;i<G[v].size();++i) {
    43             edge e=G[v][i];
    44             int d=V+e.val;
    45             if(dis[e.to]>d) {
    46                 swap(dis[e.to],d);
    47                 Q.push(P(e.to,dis[e.to]));
    48             }
    49             if(dis2[e.to]>d&&d>dis[e.to]) {
    50                 dis2[e.to]=d;
    51                 Q.push(P(e.to,dis2[e.to]));
    52             }
    53         }
    54     }
    55     return;
    56 }
    57 
    58 int hh() {
    59 //    freopen("1.in","r",stdin);
    60 //    freopen("2.out","w",stdout);
    61     read(n);read(m);
    62     for(int x;m--;) {
    63         edge p;
    64         read(x);read(p.to);read(p.val);
    65 //        --x,--p.to;
    66         G[x].push_back(p);
    67         swap(p.to,x);
    68         G[x].push_back(p);
    69     }
    70     SPFA();
    71     printf("%d
    ",dis2[n]);
    72     return 0;
    73 }
    74 
    75 int sb=hh();
    76 int main() {;}
    代码
      1 #include <ctype.h>
      2 #include <cstdio>
      3 #include <queue>
      4 
      5 const int MAXN=5010;
      6 const int MAXM=100010;
      7 const int INF=0x3f3f3f3f;
      8 
      9 int n,R,last,ans;
     10 
     11 int dis[MAXN];
     12 
     13 bool vis[MAXN];
     14 
     15 struct SKT {
     16     int v,dist;
     17     bool operator < (SKT b) const{
     18         return dist+dis[v]>b.dist+dis[b.v];
     19     }
     20 };
     21 SKT s;
     22 
     23 struct edge {
     24     int to;
     25     int val;
     26     int next;
     27     edge() {}
     28     edge(int to,int val,int next):to(to),val(val),next(next) {}
     29 };
     30 edge e[MAXM<<1];
     31 
     32 int head[MAXN],tot;
     33 
     34 inline void read(int&x) {
     35     int f=1;register char c=getchar();
     36     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
     37     for(;isdigit(c);x=x*10+c-48,c=getchar());
     38     x=x*f;
     39 }
     40 
     41 inline void add(int x,int y,int v) {
     42     e[++tot]=edge(y,v,head[x]);head[x]=tot;
     43     e[++tot]=edge(x,v,head[y]);head[y]=tot;
     44 }
     45 
     46 void SPFA() {
     47     std::queue<int> Q;
     48     for(int i=1;i<=n;++i) dis[i]=INF,vis[i]=false;
     49     dis[n]=0;
     50     vis[n]=true;
     51     Q.push(n);
     52     while(!Q.empty()) {
     53         int now=Q.front();
     54         Q.pop();
     55         vis[now]=false;
     56         for(int i=head[now];i;i=e[i].next) {
     57             int u=e[i].to;
     58             if(dis[u]>dis[now]+e[i].val) {
     59                 dis[u]=dis[now]+e[i].val;
     60                 if(!vis[u]) Q.push(u),vis[u]=true;
     61             }
     62         }
     63     }
     64     return;
     65 }
     66 
     67 void Astar() {
     68     std::priority_queue<SKT> Q;
     69     s.v=1;s.dist=0;
     70     Q.push(s);
     71     while(!Q.empty()) {
     72         SKT now=Q.top();
     73         Q.pop();
     74         if(now.v==n) ++ans,last=now.dist;
     75         if(ans==2) {
     76             printf("%d
    ",now.dist);
     77             return;
     78         }
     79         for(int i=head[now.v];i;i=e[i].next) {
     80             int u=e[i].to;
     81             SKT p=now;
     82             p.v=u;
     83             p.dist+=e[i].val;
     84             Q.push(p);
     85         }
     86     }
     87     return;
     88 }
     89 
     90 int hh() {
     91     freopen("block.in","r",stdin);
     92     freopen("block.out","w",stdout);
     93     read(n);read(R);
     94     for(int x,y,z;R--;) {
     95         read(x);read(y);read(z);
     96         add(x,y,z);
     97     }
     98     SPFA();
     99     Astar();
    100     return 0;
    101 }
    102 
    103 int sb=hh();
    104 int main() {;}
    补了一个A*


    作者:乌鸦坐飞机
    出处:http://www.cnblogs.com/whistle13326/
    新的风暴已经出现 怎么能够停止不前 穿越时空 竭尽全力 我会来到你身边 微笑面对危险 梦想成真不会遥远 鼓起勇气 坚定向前 奇迹一定会出现

     
  • 相关阅读:
    46、Spark SQL工作原理剖析以及性能优化
    45、sparkSQL UDF&UDAF
    44、开窗函数及案例
    43、内置函数及每日uv、销售额统计案例
    42、JDBC数据源案例
    41、Hive数据源复杂综合案例
    40、JSON数据源综合案例实战
    39、Parquet数据源之自动分区推断&合并元数据
    Java 的String类
    Java学习之旅基础知识篇:面向对象之封装、继承及多态
  • 原文地址:https://www.cnblogs.com/whistle13326/p/7420163.html
Copyright © 2011-2022 走看看