标签:des style blog http io ar color os sp
题目大意:给你一些点,他们可以连通,如果距离超过了d那么就要经过加油站,建立加油站的费用为第i个点是2^(i-1)。求费用最小,输出二进制表示的最小费用。
费用和sum最坏等于=2^0+2^1+……+2^(n-1)。所以最高位为0这个数字才会最小,从最高位暴力枚举如果删掉这个点之后图是连通的那么就可以删掉,否则不可以。
求图是否连通的时候可以爆搜求解,也可以并查集。
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
11 111 -1 10111011HintIn 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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <time.h>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
///#define LL long long
#define LL __int64
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007
using namespace std;
const int maxn = 210;
bool vis[maxn];
int mp[maxn][maxn];
struct node
{
int x, y;
} f[maxn];
int n, d;
double Dis(int a, int b)
{
return sqrt(((f[a].x-f[b].x)*(f[a].x-f[b].x) + (f[a].y-f[b].y)*(f[a].y-f[b].y))*1.0);
}
bool bfs()
{
bool flag[maxn];
int dis[maxn];
memset(flag, false, sizeof(flag));
for(int i = 1; i <= n; i++)
{
if(vis[i])
{
dis[i] = 0;
continue;
}
dis[i] = INF;
}
queue<int> que;
que.push(1);
flag[1] = true;
while(!que.empty())
{
int x = que.front();
que.pop();
for(int i = 1; i <= n; i++)
{
if(flag[i] || mp[x][i] > d) continue;
dis[i] = min(dis[i], dis[x]+mp[x][i]);
if(vis[i])
{
flag[i] = true;
que.push(i);
}
}
}
for(int i = 1; i <= n; i++)
{
if(vis[i] && !flag[i]) return false;
if(!vis[i] && 2*dis[i] > d) return false;
}
return true;
}
int main()
{
while(~scanf("%d %d",&n, &d))
{
for(int i = 1; i <= n; i++) scanf("%d %d",&f[i].x, &f[i].y);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
mp[i][j] = ceil(Dis(i, j));
for(int i = 1; i <= n; i++) vis[i] = true;
if(!bfs())
{
cout<<-1<<endl;
continue;
}
for(int i = n; i >= 1; i--)
{
vis[i] = false;
if(!bfs()) vis[i] = true;
}
int x = n;
while(!vis[x]) x--;
for(int i = x; i >= 1; i--) cout<<vis[i];
cout<<endl;
}
return 0;
}
HDU 4435 charge-station(暴力+判图)
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/xu12110501127/article/details/41523147