[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\n",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 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/7423489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值