zoj 3642 Just Another Information Sharing Problem

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3642


题目大意:

n(1~200)个小朋友,分享信息,第i个小朋友有ai个不同的信息,最少分享bi个,最多分享ci个。

如想小朋友i和小小朋友j分享信息,小朋友j和小朋友k分享信息,那么小朋友i和j分享的信息也和k分享,即具有传递性。

问小朋友m最多能分享到多少信息。


题目思路:

如果小朋友m有了的信息,显然不需要别人分享给m了,可以把这些信息拿出来放一边去。

因为每个小朋友都可以和m分享,所以其实就是要得出除了m有的信息,最多还能分享出多少信息。

这样其实就是二分匹配。

把出了m点外,每个点拆成ci个点,如过小朋友i有信息x,那么x向这ci个点建立边,求最大匹配。


代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define eps (1e-8)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#pragma comment(linker, "/STACK:102400000,102400000")
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
template <class T> T _abs(T x){return (x < 0)? -x:x;}
template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;}
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> void getmax(T& x,T y){x=(y > x)? y:x;}
template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;}
int TS,cas=1;
const int M=200+5;
int n,m,tot,sum,mp[1000005];
bool vis[M];
struct node{
	int a,b,c,x[11];
	void read(){
		scanf("%d%d%d",&a,&b,&c);
		for(int i=0;i<a;i++){
			scanf("%d",&x[i]);
			if(mp[x[i]]==-1) mp[x[i]]=tot++;
			x[i]=mp[x[i]];
		}
	}
}p[M];
struct hungary{
	int n,m;            //X集和Y集的点数
	int match[M*10];       //匹配
	bool vis[M*10];        //是否在增广路上
	vector<int>g[M*10];    //邻接表
	void init(int _n,int _m){
		n=_n,m=_m,clr(match,-1,m);
		for(int i=0;i<n;i++) g[i].clear();
	}
	void insert(int u,int v){g[u].push_back(v);}
	bool augment(int u){//增广
		for(int i=0,v;i<g[u].size();i++){
			if(!vis[v=g[u][i]]){
				vis[v]=1;
				if(match[v]==-1 || augment(match[v])){
					match[v]=u;
					return true;
				}
			}
		}
		return false;
	}
	int maxMatch(){
		int res=0;
		for(int i=0;i<n;i++){
			clr(vis,0,m);
			if(augment(i)) res++;
		}
		return res;
	}
}cal;

void run(){
	int i,j,k,l;
	tot=sum=0;
	clr_all(mp,-1);
	for(i=1;i<=n;i++){
		p[i].read();
		sum+=p[i].c;
	}
	scanf("%d",&m);
	clr_all(vis,0);
	for(i=0;i<p[m].a;i++) vis[p[m].x[i]]=1;
	cal.init(tot,sum);
	//printf("%d %d\n",tot,sum);
	for(i=1,l=0;i<=n;i++) if(i!=m){
		for(j=0;j<p[i].a;j++) if(!vis[p[i].x[j]]){
			for(k=0;k<p[i].c;k++){
				cal.insert(p[i].x[j],l+k);
				//printf("%d -- %d\n",p[i].x[j],l+k);
			}
		}
		l+=p[i].c;
	}
	printf("%d\n",p[m].a+cal.maxMatch());
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while((~scanf("%d",&n))) run();
    //for(scanf("%d",&TS);cas<=TS;cas++) run();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值