• [BZOJ3504][CQOI2014]危桥


    bzoj
    luogu

    sol

    很显然的建边方式:普通的桥建双向流量(inf)的边,危桥建双向流量(2)的边。
    然后分别从(a_1)(b_1)(a_2)(b_2)跑最大流,保证流量分别要大于等于(a_n)(b_n)
    多源汇且源汇一一对应的网络流是一类NPC问题,但在这里只有两源两汇,可以采用两次(Dinic)的方式,第一次(S)(a_1),(b_1)(T)(a_2),(b_2)跑最大流,第二次(S)(a_1),(b_2)(T)(a_2),(b_1)再跑一遍,只要两遍都能保证满流就行了。

    code

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int N = 60;
    const int inf = 1e9;
    struct edge{int to,nxt,w;}a[N*N<<2];
    int n,a1,a2,an,b1,b2,bn,head[N],cnt,dep[N],cur[N];
    char map[N][N];queue<int>Q;
    void link(int u,int v,int w)
    {
    	a[++cnt]=(edge){v,head[u],w};
    	head[u]=cnt;
    	a[++cnt]=(edge){u,head[v],w};
    	head[v]=cnt;
    }
    bool bfs(int s,int t)
    {
    	memset(dep,0,sizeof(dep));
    	dep[s]=1;Q.push(s);
    	while (!Q.empty())
    	{
    		int u=Q.front();Q.pop();
    		for (int e=head[u];e;e=a[e].nxt)
    			if (a[e].w&&!dep[a[e].to])
    				dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
    	}
    	return dep[t];
    }
    int dfs(int u,int f,int t)
    {
    	if (u==t) return f;
    	for (int &e=cur[u];e;e=a[e].nxt)
    		if (a[e].w&&dep[a[e].to]==dep[u]+1)
    		{
    			int tmp=dfs(a[e].to,min(a[e].w,f),t);
    			if (tmp) {a[e].w-=tmp;a[e^1].w+=tmp;return tmp;}
    		}
    	return 0;
    }
    int Dinic(int s,int t)
    {
    	int res=0;
    	while (bfs(s,t))
    	{
    		for (int i=1;i<=t;++i) cur[i]=head[i];
    		while (int tmp=dfs(s,inf,t)) res+=tmp;
    	}
    	return res;
    }
    int solve(int s1,int s2,int t1,int t2)
    {
    	int s=n+1,t=n+2;
    	memset(head,0,sizeof(head));cnt=1;
    	for (int i=1;i<n;++i)
    		for (int j=i+1;j<=n;++j)
    			if (map[i][j]=='N') link(i,j,inf);
    			else if (map[i][j]=='O') link(i,j,2);
    	link(s,s1,an<<1);link(s,s2,bn<<1);
    	link(t1,t,an<<1);link(t2,t,bn<<1);
    	return Dinic(s,t);
    }
    int main()
    {
    	while (scanf("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn)!=EOF)
    	{
    		++a1;++a2;++b1;++b2;
    		for (int i=1;i<=n;++i) scanf("%s",map[i]+1);
    		puts(solve(a1,b1,a2,b2)==2*(an+bn)&&solve(a1,b2,a2,b1)==2*(an+bn)?"Yes":"No");
    	}
    	return 0;
    }
    
  • 相关阅读:
    c++ map 的基本操作
    hdu Dragon Balls
    hdu Code Lock
    小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
    将.py脚本打包成.exe
    关于IO同步/异步/阻塞/非阻塞文章
    C++文件操作
    (十)参考教程
    (八)树控件(Tree Control),标签控件(tab control)
    (七)对话框,单选框(radiobox),复选框(checkbox),列表框(ListBox),组合框(CComboBox),水平滚动条(Horizontal scroll bar),微调(旋转)spincontrol,列表视图控件CListCtrl,静态控件static
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8613044.html
Copyright © 2020-2023  润新知