http://acm.hust.edu.cn/vjudge/contest/128352#problem/F
未ac代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm> #include <set> using namespace std; typedef long long ll; typedef unsigned long long Ull; #define MM(a,b) memset(a,b,sizeof(a)); #define CT continue #define PF printf const double eps = 1e-10; const int inf = 0x3f3f3f3f; const double pi=acos(-1); const int maxn=1e6+10; double dist[3][1005]; struct Point { double x,y,dis; void read() { scanf("%lf%lf",&x,&y); } }p[1005]; double w; int n,vis[1005]; struct edge{ int v; double l; }; vector<edge> G[1005]; double dis(Point a) { return sqrt(a.x*a.x+a.y*a.y); } Point operator-(Point a,Point b) { return (Point){a.x-b.x,a.y-b.y}; } void add_edge(int i,int j,double l) { G[i].push_back((edge){j,l}); G[j].push_back((edge){i,l}); } struct node{ int v; double dis; bool operator<(const node &a) const{ return this->dis>a.dis; } }; priority_queue<node> q; void dis_road(int k,int s,int t) { MM(vis,0); for(int i=0;i<=n+1;i++) dist[k][i]=1e12; dist[k][s]=0; while(q.size()) q.pop(); q.push((node){s,0}); while(q.size()) { node cur=q.top();q.pop(); int u=cur.v; if(dist[k][u]<cur.dis) CT; for(int i=0;i<G[u].size();i++) { int v=G[u][i].v; if(dist[k][v]>max(dist[k][u],G[u][i].l)) { dist[k][v]=max(dist[k][u],G[u][i].l); q.push((node){v,dist[k][v]}); } } } } int main() { freopen("froggy.in","r",stdin); freopen("froggy.out","w",stdout); while(~scanf("%lf%d",&w,&n)) { for(int i=1;i<=n;i++) p[i].read(); for(int i=0;i<=n+1;i++) G[i].clear(); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { double l=dis(p[j]-p[i]); add_edge(i,j,l); } for(int i=1;i<=n;i++) { add_edge(0,i,p[i].x); add_edge(n+1,i,w-p[i].x); } dis_road(0,0,n+1); dis_road(1,n+1,0); double res=w; Point a=(Point){w/2,0}; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j) { double tmp=dis(p[i]-p[j]); if(tmp/2<res) { if(dist[0][i]<res&&dist[1][j]<res) { res=max(tmp/2,max(dist[0][i],dist[1][j])); a.x=(p[i].x+p[j].x)/2; a.y=(p[i].y+p[j].y)/2; } } } for(int i=1;i<=n;i++) { double l=p[i].x,r=w-p[i].x; if(l<r) { if(res>max(r/2,dist[0][i])) { res=max(r/2,dist[0][i]); a.x=(p[i].x+w)/2; a.y=p[i].y; } } else { if(res>max(l/2,dist[1][i])) { res=max(l/2,dist[1][i]); a.x=p[i].x/2; a.y=p[i].y; } } } printf("%.9f %.9f ",a.x,a.y); } fclose(stdin); fclose(stdout); return 0; }
AC代码:
#include <iostream> #include <cstdio> #include <vector> #include <cmath> #include <iomanip> #define inf 1e10 #define maxn 1010 using namespace std; int n, w; bool ok; double backw[maxn], forw[maxn], x[maxn], y[maxn], bestAns = inf, solx, soly; double dist(int i, int j) { return sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j])); } void dijkstra(double d[]) { vector<bool> viz(maxn, false); for (int i = 1; i <= n; ++i) { int minj = -1; for (int j = 1; j <= n; ++j) { if (!viz[j] && (minj == -1 || d[j] < d[minj])) { minj = j; } } viz[minj] = true; for (int j = 1; j <= n; ++j) { d[j] = min(d[j], max(d[minj], dist(minj, j))); } } } void update(int i, int j) { double ans = max(dist(i, j)/2, max(backw[i], forw[j])); if (ans < bestAns) { bestAns = ans; solx = (x[i] + x[j])/2; soly = (y[i] + y[j])/2; } } int main() { //freopen("test.in", "r", stdin); freopen("froggy.in", "r", stdin); freopen("froggy.out", "w", stdout); cin >> w >> n; for (int i = 1; i <= n; ++i) { cin >> x[i] >> y[i]; } for (int i = 1; i <= n; ++i) { backw[i] = x[i]; } dijkstra(backw); for (int i = 1; i <= n; ++i) { forw[i] = w - x[i]; } dijkstra(forw); for (int i = 1; i <= n; ++i) { backw[0] = 0; x[0] = 0; y[0] = y[i]; update(0, i); forw[0] = 0; x[0] = w; y[0] = y[i]; update(i, 0); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { update(i, j); } } cout << fixed << setprecision(9) << solx << " " << soly; }