【最短路】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
",x/2,y/2);
160     }
161     return 0;
162 }
163 /*
164 //
165 
166 //
167 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5822483.html