zoukankan      html  css  js  c++  java
  • POJ 3255 Roadblocks

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15645   Accepted: 5504

    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

     
    题意:求次短路
    模板
    丫的无向边
    1 A*
    堆优化dijkstra + 超级读入优化+输出优化+黑科技。
    COGS 上秒杀所有
    稳居Rank 1 蛤蛤
    #include <ctype.h>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #define BUF 100000010
    #define M 100005
    #define N 5005
    char Buf[BUF],*buf=Buf;
    struct Edge
    {
        int next,to,w;
    };
    Edge edge1[M<<1],edge2[M<<1];
    using namespace std;
    int n,r,cnt,dis[N],head1[N],head2[N];
    bool vis[N];
    struct node
    {
        int to,g;
        bool operator<(node a)const
        {
            return g+dis[to]>a.g+dis[a.to];
        }
    };
    struct Node
    {
        int x,y;
        bool operator<(Node a)const
        {
            return y>a.y;
        }
    };
    inline void Read(register int &x)
    {
        for(x=0;!isdigit(*buf);++buf);
        for(;isdigit(*buf);x=x*10+*buf-'0',++buf);
    }
    inline void ins(int u,int v,int w)
    {
        edge1[++cnt].next=head1[u];
        edge1[cnt].to=v;
        edge1[cnt].w=w;
        head1[u]=cnt;
        edge2[cnt].next=head2[v];
        edge2[cnt].to=u;
        edge2[cnt].w=w;
        head2[v]=cnt;
        edge1[++cnt].next=head1[v];
        edge1[cnt].to=u;
        edge1[cnt].w=w;
        head1[v]=cnt;
        edge2[cnt].next=head2[u];
        edge2[cnt].to=v;
        edge2[cnt].w=w;
        head2[u]=cnt;
    }
    void dij(int s)
    {
        for(int i=1;i<n;++i) dis[i]=0x7fffffff;
        priority_queue<Node>q;
        dis[s]=0;
        Node a;
        a.x=s;
        a.y=dis[s];
        q.push(a);
        for(Node now;!q.empty();)
        {
            now=q.top();
            q.pop();
            if(vis[now.x]) continue;
            vis[now.x]=1;
            for(register int i=head2[now.x];i;i=edge2[i].next)
            {
                int v=edge2[i].to;
                if(dis[v]>dis[now.x]+edge2[i].w)
                {
                    dis[v]=dis[now.x]+edge2[i].w;
                    Node tmp;
                    tmp.x=v;
                    tmp.y=dis[v];
                    q.push(tmp);
                }
            }
        }
    }
    int Astar(register int s,register int t)
    {
        priority_queue<node>q;
        node a;
        a.to=s;a.g=0;
        q.push(a);
        register int ct=0;
        for(node now;!q.empty();)
        {
            now=q.top();q.pop(); 
            if(now.to==t) ct++;
            if(ct==2) return now.g;
            for(register int i=head1[now.to];i;i=edge1[i].next)
            {
                int v=edge1[i].to;
                node tmp;
                tmp.to=v;tmp.g=now.g+edge1[i].w;
                q.push(tmp);
            }
        }
        return 0;
    }
    void write(register int x)
    {
        if(x>9) write(x/10);
        putchar(x%10+'0');
    }
    int Main()
    {
    #define MINE
    #ifdef MINE
        freopen("block.in","r",stdin);
        freopen("block.out","w",stdout);
        fread(buf,1,BUF,stdin);
    #endif
        Read(n);
        Read(r);
        for(register int x,y,z;r--;)
        {
            Read(x);
            Read(y);
            Read(z);
            ins(x,y,z);
        }
        dij(n);
        write(Astar(1,n));
        return 0;
    }
    int sb=Main();
    int main(int argc,char *argv[]) {;}

    2. spfa

    #include <ctype.h>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #define M 100005
    #define N 5005
    
    struct Edge
    {
        int next,from,to,w;
        Edge (int next=0,int from=0,int to=0,int w=0) :next(next),from(from),to(to),w(w) {}
    }edge[M<<1];
    using namespace std;
    int n,r,cnt,dis1[N],dis2[N],head[N];
    bool vis[N];
    inline void Read(int &x)
    {
        bool f=0;
        register char ch=getchar();
        for(x=0;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
        for(;isdigit(ch);x=x*10+ch-'0',ch=getchar());
        x=f?-x:x;
    }
    void ins(int u,int v,int w)
    {
        edge[++cnt]=Edge(head[u],u,v,w);
        head[u]=cnt;
        edge[++cnt]=Edge(head[v],v,u,w);
        head[v]=cnt;
    }
    void spfa(int s,int *dis)
    {
        queue<int>q;
        q.push(s);
        for(int i=1;i<=n;++i) dis[i]=0x7fffffff;
        dis[s]=0;
        vis[s]=1;
        for(int now;!q.empty();)
        {
            now=q.front();
            q.pop();
            vis[now]=0;
            for(int i=head[now];i;i=edge[i].next)
            {
                int v=edge[i].to;
                if(dis[v]>dis[now]+edge[i].w)
                {
                    dis[v]=dis[now]+edge[i].w;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    int main()
    {
        Read(n);
        Read(r);
        for(int x,y,z;r--;)
        {
            Read(x);
            Read(y);
            Read(z);
            ins(x,y,z);
        }
        spfa(1,dis1);
        spfa(n,dis2);
        int Min=dis1[n],ans=0x7fffffff,dist;
        for(int i=1;i<=cnt;++i)
        {
            int u=edge[i].from,v=edge[i].to;
            dist=dis1[u]+edge[i].w+dis2[v];
            if(dist>Min&&dist<ans) ans=dist;
        }
        printf("%d\n",ans);
        return 0;
    }
  • 相关阅读:
    几个ssh和sftp的命令
    发现一个github的奇葩设定
    插耳机对orientation sensor的影响
    android中MediaPlayer类的用法
    Oracle 高性能SQL引擎剖析----执行计划
    【转】对列式数据库的一点总结和展望
    【转】大数据分析(Big Data OLAP)引擎Dremel, Tenzing 以及Impala
    TCP/IP协议详解---概述
    读取HttpWebResponse流的两种方法及注意的问题
    This project references NuGet package(s) that are missing on this computer.
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7419126.html
Copyright © 2011-2022 走看看