【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

题目链接:

  http://codeforces.com/gym/100851

题目大意:

  一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小。求这个添加的石头的位置。

题目思路:

  【最短路】

  设置源汇S,T,S到T连一条W的边,S到每个点连Xi的边,每个点到T连W-Xi的边。每个点往其他点连一条Di,j的边(直线距离)

  统计到每个点的最大跳跃距离d[i][0],d[i][1],0表示没有加过石子,1表示加过。同时记录这个答案是由哪一条边更新的。

  三种转移d[u][0]->d[v][0],d[u][0]->d[v][1],d[u][1]->d[v][1]。

  最后答案就是更新d[T][1]的那条边的终点。

  1 //
  2 //by coolxxx
  3 //#include<bits/stdc++.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<stack>
 10 #include<queue>
 11 #include<set>
 12 #include<bitset>
 13 #include<memory.h>
 14 #include<time.h>
 15 #include<stdio.h>
 16 #include<stdlib.h>
 17 #include<string.h>
 18 //#include<stdbool.h>
 19 #include<math.h>
 20 #define min(a,b) ((a)<(b)?(a):(b))
 21 #define max(a,b) ((a)>(b)?(a):(b))
 22 #define abs(a) ((a)>0?(a):(-(a)))
 23 #define lowbit(a) (a&(-a))
 24 #define sqr(a) ((a)*(a))
 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define eps (1e-8)
 28 #define J 10
 29 #define mod 1000000007
 30 #define MAX 0x7f7f7f7f
 31 #define PI 3.14159265358979323
 32 #define N 1004
 33 #define M 2100004
 34 using namespace std;
 35 typedef long long LL;
 36 int cas,cass;
 37 int n,m,lll,ans;
 38 int W,S,T;
 39 int fa[N],last[N],q[N],f[N][2];
 40 double d[N][2];
 41 bool u[N];
 42 struct Point
 43 {
 44     int x,y;
 45 }p[N];
 46 struct xxx
 47 {
 48     int from,to,next;
 49     double dis;
 50 }a[M];
 51 void add(int x,int y,double z)
 52 {
 53     a[++lll].next=last[x];
 54     a[lll].from=x;
 55     a[lll].to=y;
 56     a[lll].dis=z;
 57     last[x]=lll;
 58 }
 59 bool cmp(Point aa,Point bb)
 60 {
 61     if(aa.x!=bb.x)return aa.x>bb.x;
 62     return aa.y>bb.y;
 63 }
 64 void spfa()
 65 {
 66     int i,l=0,r=1,now,to;
 67     for(i=1;i<=T;i++)d[i][0]=d[i][1]=1e30;
 68     q[1]=S;d[S][0]=d[S][1]=0;
 69     while(l!=r)
 70     {
 71         now=q[l=(l+1)%N];
 72         if(now==T)continue;
 73         u[now]=0;
 74         if(d[now][0]>d[T][0] && d[now][1]>d[T][1] && d[now][0]>d[T][1])continue;
 75         for(i=last[now];i;i=a[i].next)
 76         {
 77             to=a[i].to;
 78             if(d[to][0]>max(d[now][0],a[i].dis))
 79             {
 80                 d[to][0]=max(d[now][0],a[i].dis);
 81                 if(d[now][0]>a[i].dis)f[to][0]=f[now][0];
 82                 else f[to][0]=i;
 83                 if(!u[to])
 84                 {
 85                     u[to]=1;
 86                     if(d[q[(l+1)%N]][0]>d[to][0] && d[q[(l+1)%N]][1]>d[to][1])
 87                         q[l]=to,l=(l+N-1)%N;
 88                     else q[r=(r+1)%N]=to;
 89                 }
 90             }
 91             if(d[to][1]>max(d[now][1],a[i].dis))
 92             {
 93                 d[to][1]=max(d[now][1],a[i].dis);
 94                 f[to][1]=f[now][1];
 95                 if(!u[to])
 96                 {
 97                     u[to]=1;
 98                     if(d[q[(l+1)%N]][0]>d[to][0] && d[q[(l+1)%N]][1]>d[to][1])
 99                         q[l]=to,l=(l+N-1)%N;
100                     else q[r=(r+1)%N]=to;
101                 }
102             }
103             if(d[to][1]>max(d[now][0],a[i].dis/2))
104             {
105                 d[to][1]=max(d[now][0],a[i].dis/2);
106                 f[to][1]=i;
107                 if(!u[to])
108                 {
109                     u[to]=1;
110                     if(d[q[(l+1)%N]][0]>d[to][0] && d[q[(l+1)%N]][1]>d[to][1])
111                         q[l]=to,l=(l+N-1)%N;
112                     else q[r=(r+1)%N]=to;
113                 }
114             }
115         }
116     }
117 }
118 int main()
119 {
120     #ifndef ONLINE_JUDGE
121 //    freopen("froggy.in","r",stdin);
122 //    freopen("froggy.out","w",stdout);
123     #endif
124     int i,j,k;
125     double x,y,z;
126 //    for(scanf("%d",&cass);cass;cass--)
127 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
128 //    while(~scanf("%s",s+1))
129     while(~scanf("%d",&W))
130     {
131         lll=0;mem(last,0);mem(f,0);mem(u,0);
132         scanf("%d",&n);
133         S=n+1,T=n+2;p[S].x=p[S].y=p[T].x=p[T].y=0;
134         add(S,T,W);
135         for(i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
136         sort(p+1,p+1+n,cmp);
137         for(i=1;i<=n;i++)
138         {
139             add(S,i,p[i].x);
140             add(i,T,W-p[i].x);
141         }
142         for(i=1;i<n;i++)
143         {
144             for(j=i+1;j<=n;j++)
145             {
146                 z=sqrt(1LL*(p[i].x-p[j].x)*(p[i].x-p[j].x)+1LL*(p[i].y-p[j].y)*(p[i].y-p[j].y));
147                 if(z>W)continue;
148                 add(i,j,z);
149                 add(j,i,z);
150             }
151         }
152         spfa();
153         j=a[f[T][1]].from;
154         k=a[f[T][1]].to;
155         if(j==S)p[j].x=0,p[j].y=p[k].y;
156         if(k==T)p[k].x=W,p[k].y=p[j].y;
157         x=1LL*(p[j].x+p[k].x);
158         y=1LL*(p[j].y+p[k].y);
159         printf("%lf %lf\n",x/2,y/2);
160     }
161     return 0;
162 }
163 /*
164 //
165
166 //
167 */

时间: 2024-11-10 06:51:06

【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)的相关文章

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 439    Accepted Submission(s): 157 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 309 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 291    Accepted Submission(s): 160 Problem Description On an isolated island, lived some dwarves. A king (not a dwarf) ruled t

HDU 5785 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 976    Accepted Submission(s): 375 Problem Description The shorter, the simpler. With this problem, you should be convinced of this tru

2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5984 Pocky 题意 给出一根棒子(可以吃的)的长度x和切割过程中不能小于的长度d,每次随机的选取一个位置切开,吃掉左边的一半,对右边的棒子同样操作,直至剩余的长度不大于d时停止.现在给出x和d,问切割次数的数学期望是多少. 解题思路 当看到第二个样例2 1时,结果是1.693147,联想到ln

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 269 Problem Description hannnnah_j is a teacher in WL High school who teaches biolog