• [SHOI2003] 吃豆豆


    \(Solution\)

    考虑把一个豆豆看成一条边,那限制流量为\(1\),费用为\(1\),豆豆即会被吃一次。拆点,两点之间连一条有贡献的边和无贡献的边,可做到让两条路径重合后不会多产生贡献。路线不可以相交可以通过连边的方式解决,但我们发现边数太多了。考虑我们连的一些无用的边,形如一个钝角三角形,明显有边\(AC\)是可以不连的,优化后即可过。

    \(Code\)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N = 4005,inf = 2e9;
    int h[N],S,T,n,flow[N],vis[N],q[N * 30],tot = 1,pre[N],up[N],m,dis[N],S2,in[N];
    
    struct nd{
    	int x,y;
    }a[N];
    struct edge{
    	int to,nxt,f,z;
    }e[1000005];
    bool cmp(nd x,nd y)
    {
    	if (x.x == y.x) return x.y < y.y;
    	return x.x < y.x;
    }
    void add(int x,int y,int f,int z)
    {
    	e[++tot] = edge{y,h[x],f,z},h[x] = tot;
    	e[++tot] = edge{x,h[y],0,-z},h[y] = tot;
    }
    int spfa()
    {
    	memset(flow,127,sizeof flow);
    	memset(dis,127,sizeof dis);
    	memset(vis,0,sizeof vis);
    	int head = 0,tail = 1;
    	q[1] = S,dis[S] = 0,vis[S] = 1,pre[T] = -1;
    	while (head < tail)
    	{
    		int u = q[++head]; vis[u] = 0;
    		for (int i = h[u]; i; i = e[i].nxt)
    		{
    			int v = e[i].to;
            	if (dis[v] > dis[u] + e[i].z && e[i].f > 0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    			{
    				dis[v] = dis[u] + e[i].z,pre[v] = u,up[v] = i;
    				flow[v] = min(flow[u],e[i].f);
    				if (!vis[v]) q[++tail] = v,vis[v] = 1; 
    			}
    		}
    	}
    	return (pre[T] != -1);
    }
    int main()
    {
    	scanf("%d",&n);
    	S = n * 2 + 1,S2 = S + 1,T = S + 2,add(S,S2,2,0);
    	for (int i = 1; i <= n; i++) scanf("%d%d",&a[i].x,&a[i].y);
    	sort(a + 1,a + 1 + n,cmp);
    	for (int i = 2; i <= n; i++)
    		for (int j = i - 1,g = -1; j >= 1; j--)
    		{
    			if (a[j].y > a[i].y) continue;
    			if (a[j].y <= g) continue;
    			g = a[j].y,add(n + j,i,2,0),in[i]++;
    		}
    	for (int i = 1; i <= n; i++) if (!in[i]) add(S2,i,2,0); //不这样会TLE
    	for (int i = 1; i <= n; i++) add(i,i + n,1,-1),add(i,i + n,1,0),add(i + n,T,2,0);
    	int ansc = 0;
    	for (; spfa();)
    	{
    		int o = T; ansc += flow[T] * dis[T];
    		for (; o != S; o = pre[o]) e[up[o]].f -= flow[T],e[up[o] ^ 1].f += flow[T];
    	}
    	printf("%lld\n",-ansc);
    }
    
  • 相关阅读:
    WEB 3D SVG CAD 向量 几个实施(转)
    Dos.Common
    dos.orm
    php程序员招聘
    python之禅
    Electron
    MongoDB主从复制
    OSG开发概览
    从1-100中隔5个数去掉一个数,最后去掉的哪个数(如果数过一遍了从前端接着开始)?
    django/python日志logging 的配置以及处理
  • 原文地址:https://www.cnblogs.com/nibabadeboke/p/15848041.html
Copyright © 2020-2023  润新知