牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A
题意:
恬恬的生日临近了。宇扬给她准备了一个蛋糕。
正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为m个区域。因为某种原因,他必须把第i根蜡烛插在第ai个区域或第bi个区域。区域之间是不相交的。宇扬在一个区域内同时摆放x支蜡烛就要花费x2的时间。宇扬布置蛋糕所用的总时间是他在每个区域花的时间的和。
宇扬想快些见到恬恬,你能告诉他布置蛋糕最少需要多少时间吗?
正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为m个区域。因为某种原因,他必须把第i根蜡烛插在第ai个区域或第bi个区域。区域之间是不相交的。宇扬在一个区域内同时摆放x支蜡烛就要花费x2的时间。宇扬布置蛋糕所用的总时间是他在每个区域花的时间的和。
宇扬想快些见到恬恬,你能告诉他布置蛋糕最少需要多少时间吗?
思路:
建立图,左边n个点,表示不同的蜡烛,右边m个点,表示不同的区域,根据ai和bi从左边向右边连容量为1,费用为0的边,右边每个m点都向汇点连n条边,容量为1,费用为多一个蜡烛的增量。左边还要从源点拉来容量为1,费用为0的边。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <iomanip> #include <cstdlib> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <cctype> #include <queue> #include <cmath> #include <list> #include <map> #include <set> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int ,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //2147483647 const ll nmos = 0x80000000LL; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3fLL; //18 const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } // #define _DEBUG; //*// #ifdef _DEBUG freopen("input", "r", stdin); // freopen("output.txt", "w", stdout); #endif /*-----------------------show time----------------------*/ const int maxn = 1e6+9; struct Edge { int to,val,cost,nxt; }gEdge[maxn]; int gHead[maxn],gPre[maxn]; int gPath[maxn],gDist[maxn]; bool in[maxn]; int gcount = 0; int n,m,k,w; int fac[maxn]; bool spfa(int s,int t){ memset(gPre, -1, sizeof(gPre)); memset(gDist,inf,sizeof(gDist)); memset(in, false , sizeof(in)); gDist[s] = 0; in[s] = true; queue<int>q; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); in[u] = false; for(int e = gHead[u]; e!=-1; e = gEdge[e].nxt){ int v = gEdge[e].to, w = gEdge[e].cost; if(gEdge[e].val > 0 && gDist[v] > gDist[u] + w){ gDist[v] = gDist[u] + gEdge[e].cost; gPre[v] = u; gPath[v] = e; if(!in[v]){ q.push(v);in[v] = true; } } } } if(gPre[t] == -1)return false; return true; } int MinCostFlow(int s,int t){ int cost = 0,flow = 0; while(spfa(s,t)){ int f = inf; for(int u = t; u != s; u = gPre[u]){ if(gEdge[gPath[u]].val < f){ f =gEdge[gPath[u]].val; } } flow += f; cost += gDist[t] * f; for(int u=t; u!=s; u = gPre[u]){ gEdge[gPath[u]].val -= f; gEdge[gPath[u] ^ 1].val += f; } } return cost; } void addedge(int u,int v,int val, int cost){ gEdge[gcount].to = v; gEdge[gcount].val = val; gEdge[gcount].cost = cost; gEdge[gcount].nxt = gHead[u]; gHead[u] = gcount++; gEdge[gcount].to = u; gEdge[gcount].val = 0; gEdge[gcount].cost = -cost; gEdge[gcount].nxt = gHead[v]; gHead[v] = gcount++; } struct eee { int l,r,w,op; }e[maxn]; /* 是大源点,m+m+1是ci源点,m+m+2是终点。 */ void solve(){ memset(gHead,-1,sizeof(gHead)); gcount = 0; scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) fac[i] = i*i; for(int i=n; i>=1; i--) fac[i] = fac[i] - fac[i-1]; for(int i=1; i<=n; i++){ addedge(0,i,1,0); } for(int i=1; i<=n; i++){ int le,ri; scanf("%d%d", &le, &ri); addedge(i,le + n, 1, 0); addedge(i,ri + n, 1, 0); } for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ addedge(i+n,n+m+1,1,fac[j]); } } printf("%d ",MinCostFlow(0,n+m+1)); } int main(){ solve(); return 0; }