最大流建图比较容易第一次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 == 0 ? b : gcd(b, a % b);} #define MAXN 220 const int INF = 0x3f3f3f3f ; struct node { int u,v,next; int cap,flow; }edge[MAXN * MAXN * 4]; struct Dicnc { int source,target; bool vis[MAXN]; int d[MAXN],cur[MAXN],head[MAXN],cnt; int n; void init(int x) { memset(head,-1,sizeof(head)); cnt = 0; 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 = 0; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].u = v; edge[cnt].v = u; edge[cnt].flow = 0; edge[cnt].cap = 0; edge[cnt].next = head[v]; head[v] = cnt++; } bool BFS() { memset(vis,false,sizeof(vis)); queue<int>q; d[source] = 0; vis[source] = true; q.push(source); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i != -1; 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] + 1; q.push(e.v); } } } return vis[target]; } int DFS(int x,int a) { if (x == target || a == 0) return a; int flow = 0,f; for (int & i = cur[x]; i != -1; i = edge[i].next) { node & e = edge[i]; if (d[x] + 1 == d[e.v] && (f = DFS(e.v,min(a,e.cap - e.flow))) > 0) { e.flow += f; edge[i ^ 1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } int Maxflow(int S,int T) { source = S; target = T; int flow = 0; while (BFS()) { for(int i=0; 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 = 0; for (int i = 1; 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 = 1; k <= N; k++) { ans.init(2 * N + 1); for (int i = 1; i <= N; i++) { ans.addedge(0,i,p[i].a); ans.addedge(i,i + N,p[i].b); } for (int i = 1; i <= N; i++) for (int j = i + 1; j <= N; j++) if (dist(i,j) <= D) { ans.addedge(i + N,j,INF); ans.addedge(j + N,i,INF); } int temp = ans.Maxflow(0,k); if (sum == temp) { if (!first) {printf("%d",k - 1);first = true;} else printf(" %d",k - 1); } } if (!first) printf("-1"); puts(""); } return 0; }