[BJOI2012]連連看 BZOJ2661 費用流
阿新 • • 發佈:2019-02-06
-- node sample style dig pop rep name pri
輸出樣例#1: 復制
題目描述
凡是考智商的題裏面總會有這麽一種消除遊戲。不過現在面對的這關連連看可不是QQ遊戲裏那種考眼力的遊戲。我們的規則是,給出一個閉區間[a,b]中的全部整數,如果其中某兩個數x,y(設x>y)的平方差x^2-y^2是一個完全平方數z^2,並且y與z互質,那麽就可以將x和y連起來並且將它們一起消除,同時得到x+y點分數。那麽過關的要求就是,消除的數對盡可能多的前提下,得到足夠的分數。快動手動筆算一算吧。
輸入輸出格式
輸入格式:只有一行,兩個整數,分別表示a,b。
輸出格式:兩個數,可以消去的對數,及在此基礎上能得到的最大分數。
輸入輸出樣例
輸入樣例#1: 復制1 15
2 34
說明
對於30%的數據,1<=a,b<=100
對於100%的數據,1<=a,b<=1000
將每個點分為in,out;
建邊的時候要註意我們消去的是一對數字;
所以建邊的時候Inx----Outy,Iny----Outx都要建邊;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 100005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) #define mclr(x,a) memset((x),a,sizeof(x)) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-5 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline int rd() { int x = 0; char c = getchar(); bool f = false; while (!isdigit(c)) { if (c == ‘-‘) f = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; } /*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) { if (!b) { x = 1; y = 0; return a; } ans = exgcd(b, a%b, x, y); ll t = x; x = y; y = t - a / b * y; return ans; } */ bool vis[maxn]; int n, m, s, t; int x, y, f, z; int dis[maxn], pre[maxn], last[maxn], flow[maxn]; int maxflow, mincost; struct node { int to, nxt, flow, dis; }edge[maxn << 2]; int head[maxn], cnt; queue<int>q; void addedge(int from, int to, int flow, int dis) { edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].dis = dis; edge[cnt].nxt = head[from]; head[from] = cnt; } bool spfa(int s, int t) { memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow)); ms(vis); q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for (int i = head[now]; i != -1; i = edge[i].nxt) { if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = edge[i].dis + dis[now]; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now], edge[i].flow); if (!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } return pre[t] != -1; } void mincost_maxflow() { while (spfa(s, t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while (now != s) { edge[last[now]].flow -= flow[t]; edge[last[now] ^ 1].flow += flow[t]; now = pre[now]; } } } bool OK(int x, int y) { if (x < y)swap(x, y); int Z = x * x - y * y; int z = (int)sqrt(Z); if (z*z == Z) { if (gcd(z, y) == 1) { return true; } else return false; } else return false; } int main() { // ios::sync_with_stdio(0); mclr(head, -1); cnt = 1; int a, b; a = rd(); b = rd(); s = 0; t = b + 1; int num = b - a + 1; for (int i = a; i <= b; i++) { addedge(s, i, 1, 0); addedge(i, s, 0, 0); addedge(i + b , t, 1, 0); addedge(t, i + b , 0, 0); } for (int i = a; i <= b; i++) { for (int j = i + 1; j <= b; j++) { if (OK(i, j)) { addedge(i + b, j, 0, i + j); addedge(j, i + b, 1, -(i + j)); addedge(j + b, i, 0, i + j); addedge(i, j + b, 1, -(i + j)); } } } mincost_maxflow(); printf("%d %d\n", maxflow/2, -mincost/2); return 0; }
[BJOI2012]連連看 BZOJ2661 費用流