hdu 4435 charge-station(最短路+思维,5级)

E - charge-station
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4435
Appoint description:  System Crawler  (2013-10-04)

Description

There are n cities in M^3's empire. M^3 owns a palace and a car and the palace resides in city 1. One day, she wants to travel around all the cities from her palace and finally back to her home. However, her car has limited energy and can only travel by no more than D meters. Before it was run out of energy, it should be charged in some oil station. Under M^3's despotic power, the judge is forced to build several oil stations in some of the cities. The judge must build an oil station in city 1 and building other oil stations is up to his choice as long as M^3 can successfully travel around all the cities. 
Building an oil station in city i will cost 2  i-1 MMMB. Please help the judge calculate out the minimum cost to build the oil stations in order to fulfill M^3's will. 
 

Input

There are several test cases (no more than 50), each case begin with two integer N, D (the number of cities and the maximum distance the car can run after charged, 0 < N ≤ 128). 
Then follows N lines and line i will contain two numbers x, y(0 ≤ x, y ≤ 1000), indicating the coordinate of city i. 
The distance between city i and city j will be ceil(sqrt((xi - xj)  2 + (yi - yj)  2)). (ceil means rounding the number up, e.g. ceil(4.1) = 5) 
 

Output

For each case, output the minimum cost to build the oil stations in the binary form without leading zeros. 
If it's impossible to visit all the cities even after all oil stations are build, output -1 instead. 
 

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();
  }
}




你可能感兴趣的:(hdu 4435 charge-station(最短路+思维,5级))