EK WA 10pts
查看原帖
EK WA 10pts
482610
Mortidesperatslav楼主2024/7/12 19:41

我的 EK 过了方格取数加强版,但是本题 WA。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a, b;
map<int, int> mp;
int n, m, s, t, h[10005], zd[100005], ed[100005], nxt[100005], vis[10005], inc[10005], pr[10005], cnt, mxf;
int d[10005], co[100005], ans;
queue<int> q;
void add(int u, int v, int c, int w){
	zd[++cnt]=v,ed[cnt]=c,co[cnt]=w,nxt[cnt]=h[u],h[u]=cnt;
	zd[++cnt]=u,co[cnt]=-w,nxt[cnt]=h[v],h[v]=cnt;
}
bool spfa(){
	memset(vis,0,sizeof(vis));
	memset(d,-0x3f3f3f3f,sizeof(d));
	while(!q.empty())q.pop();
	q.push(s);
	d[s]=0;
	vis[s]=1;
	inc[s]=LLONG_MAX;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=h[u];i;i=nxt[i])
			if(ed[i]){
				int v=zd[i];
				if(d[v]<d[u]+co[i]){
					//cout<<v<<" "<<d[v]<<" "<<d[u]<<" "<<co[i]<<"\n";
					d[v]=d[u]+co[i];
					//cout<<d[v]<<"\n";
					inc[v]=min(inc[u],ed[i]);
					pr[v]=i;
					if(!vis[v]){
						vis[v]=1;
						q.push(v);
					}
				}
			}
		vis[u]=0;
	}
	if(d[t]<=-0x3f3f3f3f)return 0;
	return 1;
}
void upd(){
	int u=t;
	while(u!=s){
		int i=pr[u];
		ed[i]-=inc[t];
		ed[i^1]+=inc[t];
		u=zd[i^1];
	}
	mxf+=inc[t];
	ans+=d[t]*inc[t];
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> a >> b;
	s = 1;
	t = b + b + 2;
	for (int i = 1; i <= 1000; i++)
		mp[i * i] = i;
	for (int i = a; i <= b; i++) {
		add(s, i + 1, 1, 0);
		add(i + b + 1, t, 1, 0);
	}
	for (int i = a; i <= b; i++)
		for (int j = i + 1; j <= b; j++) {
			if (mp[j * j - i * i] && __gcd(i, mp[j * j - i * i]) == 1) {
				add(j, i + b + 1, 1, i + j);
				add(i, j + b + 1, 1, i + j);
			}
		}
	while (spfa())
		upd();
	cout << (mxf >> 1) << ' ' << (ans >> 1);
}
2024/7/12 19:41
加载中...