Description
Input
Output
Sample Input
3 3 0 0 0 3 0 1 3 2 0 0 0 3 0 1 3 1 0 0 0 3 0 1 16 23 30 40 37 52 49 49 52 64 31 62 52 33 42 41 52 41 57 58 62 42 42 57 27 68 43 67 58 48 58 27 37 69
Sample Output
11 111 -1 10111011
Hint
In case 1, the judge should select (0, 0) and (0, 3) as the oil station which result in the visiting route: 1->3->2->3->1. And the cost is 2^(1-1) + 2^(2-1) = 3.
思路:2^n > sum(2^(i))i->0-n-1 所以不标最大,看可行不?不可行,则该点必须建站。1为起始点入队,有油的距离设为0.跑最短路。
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<queue> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) using namespace std; const int mp=133; const int oo=1e9; class Point { public:float x,y; }f[mp]; int g[mp][mp],N,D; int get_dis(int x,int y) { if(x==y)return 0; return ceil(sqrt((f[x].x-f[y].x)*(f[x].x-f[y].x)+(f[x].y-f[y].y)*(f[x].y-f[y].y))); } class Edge { public:int v,next,w; }; class ShortPath { public: int dis[mp]; bool vis[mp],oil[mp]; int head[mp];Edge e[mp*mp*2];int edge; void clear() { clr(head,-1);edge=0; } void add(int u,int v,int w) { e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++; } bool spfa() { queue<int>Q; clr(vis,0); FOR(i,1,N) if(oil[i]){dis[i]=0;} else dis[i]=oo; vis[1]=1;Q.push(1); while(!Q.empty()) { int u=Q.front();Q.pop(); for(int i=head[u];~i;i=e[i].next) { int v=e[i].v; if(!vis[v]&&e[i].w<=D)///油站能否互相可达 { dis[v]=min(dis[v],dis[u]+e[i].w); //vis[v]=1; if(oil[v]){Q.push(v);vis[v]=1;} } } } FOR(i,1,N) if(oil[i]&&!vis[i])return 0; else if(!oil[i]&&dis[i]+dis[i]>D)return 0; return 1; } void getans() { FOR(i,1,N) oil[i]=1; if(!spfa()){printf("-1\n");return;} for(int i=N;i>=2;--i) { oil[i]=0; if(spfa())continue; else oil[i]=1; } bool flag=0; for(int i=N;i>=1;--i) if(oil[i]&&!flag){flag=1;putchar('1');} else if(flag) { if(oil[i])putchar('1'); else putchar('0'); } putchar('\n'); } }sp; int main() { while(~scanf("%d%d",&N,&D)) { sp.clear(); FOR(i,1,N)scanf("%f%f",&f[i].x,&f[i].y); FOR(i,1,N)FOR(j,1,N)if(i!=j)sp.add(i,j,get_dis(i,j));//g[i][j]=get_dis(i,j); sp.getans(); } }