zoukankan      html  css  js  c++  java
  • [POI 2004]ZAW

    Description

    Byte 山的山脚下有一个洞穴入口. 这个洞穴由复杂的洞室经过隧道连接构成. 洞穴的入口是 1 号点.两个洞室要么就通过隧道连接起来,要么就经过若干隧道间接的相连. 现在决定组织办一个'King's of Byteotia Cup' 比赛. 参赛者的目标就是任意选择一条路径进入洞穴并尽快出来即可. 一条路径必须经过除了 1 之外还至少要经过其他一个洞室.一条路径中一个洞不能重复经过(除了 1 以外),类似的一条隧道也不能重复经过.

    一个著名的洞穴探险家 Byteala 正准备参加这个比赛. Byteala 已经训练了数月而且他已获得了洞穴系统的一套详细资料. 对于每条隧道他都详细计算了从两个方向经过所需要的时间. 经过一个洞室的时间很短可以忽略不记. 现在Byteala 向计算一条符合条件的最优路径.

    Input

    第一行有两个数 n m (3 <= n <= 5000, 3 <= m <= 10000) 分别表示洞室的数目以及连接他们的隧道的数目. 洞室从 1 n 编号. “前面洞室的编号为 1.

    接下来 m 行描述了所有的隧道. 每行四个整数 a,b,c,d 表示从洞室 a 到洞室 b 需要 c分钟的时间,而从洞室 b到洞室 a需要 d分钟的时间, 1 <= a,b <= n, a <> b, 1 <= c,d <= 10000. 你可以假设符合要求的路径肯定存在.

    Output

    输出一行,最少需要多少时间完成比赛.

    Sample Input

    3 3
    1 2 4 3
    2 3 4 2
    1 3 1 1

    Sample Output

    6

    HINT

    经过 1231

    题解

    要求一个最短路,担心的就是一条边被正反经过两次。

    规定第一步为$1$到$i$,并把这条边设为不可经过。然后从$i$做最短路到$1$,因为这个过程是不会经历重边的(如果经历了就不是最短路了)。

    数据有点坑:不要标记数组还快一点...

    数据有点卡$SPFA$:如果松弛节点时算出的$dist$比之前算出的最优$ans$还大,显然不用拓展了。

     1 #include<map>
     2 #include<cmath>
     3 #include<ctime>
     4 #include<queue>
     5 #include<stack>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<string>
     9 #include<cstdlib>
    10 #include<cstring>
    11 #include<iostream>
    12 #include<algorithm>
    13 #define LL long long
    14 #define Max(a,b) ((a)>(b) ? (a):(b))
    15 #define Min(a,b) ((a)<(b) ? (a):(b))
    16 using namespace std;
    17 const int N=5000;
    18 const int M=10000;
    19 const int lenth=50000;
    20 const int INF=~0u>>1;
    21 
    22 int n,m,u,v,c;
    23 struct tt
    24 {
    25     int to,next,cost;
    26 }edge[M*2+5];
    27 int path[N+5],top=-1;
    28 void Add(int u,int v,int c);
    29 int ans=INF;
    30 int tmp,tmp2;
    31 
    32 int dist[N+5];
    33 void SPFA(int u);
    34 
    35 int main()
    36 {
    37     freopen("zaw.in","r",stdin);
    38     freopen("zaw.out","w",stdout);
    39     memset(path,-1,sizeof(path));
    40     scanf("%d%d",&n,&m);
    41     for (int i=1;i<=m;i++)
    42     {
    43         scanf("%d%d%d",&u,&v,&c);
    44         Add(u,v,c);
    45         scanf("%d",&c);
    46         Add(v,u,c);
    47     }
    48     for (int i=path[1];i!=-1;i=edge[i].next)
    49     {
    50         tmp=edge[i].cost;
    51         tmp2=edge[i^1].cost;
    52         edge[i].cost=edge[i^1].cost=INF-1e9;
    53         SPFA(edge[i].to);
    54         ans=Min(ans,tmp+dist[1]);
    55         edge[i].cost=tmp;
    56         edge[i^1].cost=tmp2;
    57     }
    58     printf("%d
    ",ans);
    59     return 0;
    60 }
    61 
    62 void Add(int u,int v,int c)
    63 {
    64     edge[++top].to=v;
    65     edge[top].cost=c;
    66     edge[top].next=path[u];
    67     path[u]=top;
    68 }
    69 void SPFA(int u)
    70 {
    71     memset(dist,127/3,sizeof(dist));
    72     dist[u]=0;
    73     int Q[lenth+5],head=0,tail=1;
    74     Q[head]=u;
    75     while (head!=tail)
    76     {
    77         int u=Q[head++];
    78         head%=lenth;
    79         for (int i=path[u];i!=-1;i=edge[i].next)
    80         {
    81             if (dist[edge[i].to]>dist[u]+edge[i].cost&&ans>tmp+dist[u]+edge[i].cost)
    82             {
    83                 dist[edge[i].to]=dist[u]+edge[i].cost;
    84                 Q[tail++]=edge[i].to;
    85                 tail%=lenth;
    86             }
    87         }
    88     }
    89 }
  • 相关阅读:
    转载:CentOS7下部署Django项目详细操作步骤
    转载 js弹出框、对话框、提示框、弹窗总结
    python 博客开发之散乱笔记
    python 用 PIL 模块 画验证码
    RL
    c++ 基础知识 0001 const 知识2
    c++ 基础知识 0001 const 知识1
    神经网络的理论基础
    go set up on ubuntu
    ubuntu 上查看文件的内容,二进制形式展现
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7423489.html
Copyright © 2011-2022 走看看