最大流建图比较容易第一次Dicnc抄了下别人的版

存一下以后方便查

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == ? b : gcd(b, a % b);}
#define MAXN 220
const int INF = 0x3f3f3f3f ;
struct node
{
int u,v,next;
int cap,flow;
}edge[MAXN * MAXN * ];
struct Dicnc
{
int source,target;
bool vis[MAXN];
int d[MAXN],cur[MAXN],head[MAXN],cnt;
int n;
void init(int x)
{
memset(head,-,sizeof(head));
cnt = ;
n = x;
}
void addedge(int u ,int v, int cap)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = cap;
edge[cnt].flow = ;
edge[cnt].next = head[u];
head[u] = cnt++; edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].flow = ;
edge[cnt].cap = ;
edge[cnt].next = head[v];
head[v] = cnt++;
}
bool BFS()
{
memset(vis,false,sizeof(vis));
queue<int>q;
d[source] = ;
vis[source] = true;
q.push(source);
while (!q.empty())
{
int u = q.front(); q.pop();
for (int i = head[u]; i != -; i = edge[i].next)
{
node & e = edge[i];
if (!vis[e.v] && e.cap > e.flow)
{
vis[e.v] = true;
d[e.v] = d[u] + ;
q.push(e.v);
}
}
}
return vis[target];
}
int DFS(int x,int a)
{
if (x == target || a == ) return a;
int flow = ,f;
for (int & i = cur[x]; i != -; i = edge[i].next)
{
node & e = edge[i];
if (d[x] + == d[e.v] && (f = DFS(e.v,min(a,e.cap - e.flow))) > )
{
e.flow += f;
edge[i ^ ].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int Maxflow(int S,int T)
{
source = S;
target = T;
int flow = ;
while (BFS())
{
for(int i=; i<=n; i++) cur[i]=head[i];
flow += DFS(source,INF);
//cout<<flow<<endl;
}
return flow;
}
};
struct point
{
int x,y;
int a,b;
}p[MAXN];
double dist(int i,int j)
{
double a=(p[i].x-p[j].x)*(p[i].x-p[j].x)*1.0;
double b=(p[i].y-p[j].y)*(p[i].y-p[j].y)*1.0;
return sqrt(a+b);
}
Dicnc ans;
int N;
double D;
int main()
{
//freopen("sample.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%lf",&N,&D);
int sum = ;
for (int i = ; i <= N; i++) { scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].a,&p[i].b); sum += p[i].a;}
bool first = false;
//printf("%d\n",sum);
for (int k = ; k <= N; k++)
{
ans.init( * N + );
for (int i = ; i <= N; i++)
{
ans.addedge(,i,p[i].a);
ans.addedge(i,i + N,p[i].b);
}
for (int i = ; i <= N; i++)
for (int j = i + ; j <= N; j++)
if (dist(i,j) <= D)
{
ans.addedge(i + N,j,INF);
ans.addedge(j + N,i,INF);
}
int temp = ans.Maxflow(,k);
if (sum == temp)
{
if (!first) {printf("%d",k - );first = true;}
else printf(" %d",k - );
}
}
if (!first) printf("-1");
puts("");
}
return ;
}

UVALIVE 3972 March of the Penguins的更多相关文章

  1. [POJ 3498] March of the Penguins

    March of the Penguins Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4378   Accepted:  ...

  2. poj 3498 March of the Penguins(拆点+枚举汇点 最大流)

    March of the Penguins Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 4873   Accepted: ...

  3. POJ 3498 March of the Penguins(网络最大流)

    Description Somewhere near the south pole, a number of penguins are standing on a number of ice floe ...

  4. hdu 2334 March of the Penguins

      题意大意 在X,Y坐标系中有N(N<=100)个冰块,有些冰块上有1若干只企鹅,每只企鹅一次最多跳M距离,一个冰块在有Mi个企鹅离开,就会消失,问有哪些冰块可以作为集合点,就是所有企鹅都能成 ...

  5. March of the Penguins

    poj3498:http://poj.org/problem?id=3498 题意:某个冰块上有a只企鹅,总共可以跳出去b只,问是否可能所有的企鹅都跳到某一块冰块上,输出所有的可能的冰块的编号. 由于 ...

  6. poj 3498 March of the Penguins(最大流+拆点)

    题目大意:在南极生活着一些企鹅,这些企鹅站在一些冰块上,现在要让这些企鹅都跳到同一个冰块上.但是企鹅有最大的跳跃距离,每只企鹅从冰块上跳走时会给冰块造成损害,因此企鹅跳离每个冰块都有次数限制.找出企鹅 ...

  7. UVA 12125 March of the Penguins

    题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...

  8. UVALive-3972 March of the Penguins (最大流:节点容量)

    题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...

  9. POJ3498:March of the Penguins——题解

    最近的题解的故事背景割. 题目: 描述 在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃, ...

随机推荐

  1. C#学习你需要知道的---(For和Foreach)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52577283 作者:car ...

  2. android 获取图片

    Android获取手机或者内存卡里面的图片有两种方式 1.这是通过一种action Intent intent=new Intent(); intent.setAction(Intent.ACTION ...

  3. python 表格存取方法(转)

    xlwt/xlrd库 存Excel文件:(如果存储数据中有字符,那么写法还有点小小的变化) import xlwt workbook = xlwt.Workbook(encoding='utf-8') ...

  4. interface in iOS

    lo = localhosten = ethernetap = Probably for access point (if you are acting as a wifi host) pdp_ip ...

  5. svn服务器的配置和使用

    安装好了svn服务器,打开 VisualSVN Server Manager 先新建用户和组,在代码库创建的时候可以指定哪些用户或者组有读写权限,也可以创建好后指定 创建用户 输入用户名和密码创建用户 ...

  6. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

  7. 《Cracking the Coding Interview》——第2章:链表——题目6

    2014-03-18 02:41 题目:给定一个带有环的单链表,找出环的入口节点. 解法1:用hash来检测重复节点肯定是容易想而且效率也高的好办法. 代码: // 2.6 You have a ci ...

  8. USACO Section1.2 Milking Cows 解题报告

    milk2解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  9. wget下载https文件,服务器可以虚拟机中不行的问题

    用wget下载一个图片资源(https协议),在服务器上可以,但在本机的虚拟机中卡在下面这里了: [root@localhost ~]# wget 'https://gp1.wac.edgecastc ...

  10. diskimage-builder-command

    yum -y install python-virtualenv.noarch virtualenv ~/dib-virtualenv . ~/dib-virtualenv/bin/activate ...