예제 #1
0
파일: 218.cpp 프로젝트: Abhay4/Algorithm
int main() {
	scanf("%d", &n);
	int left = inf, right = -inf;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			scanf("%d", &mat[i][j]);
			if (mat[i][j] < left) left = mat[i][j];
			if (mat[i][j] > right) right = mat[i][j];
		}
	}
	while (left <= right) {
		int mid = (left + right) / 2;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (mat[i][j] <= mid) mm[i][j] = 1;
				else mm[i][j] = 0;
			}
		}
		int ret = hungary(n, n, mm, match1, match2);
		if (ret == 1) right = mid - 1;
		else left = mid + 1;
	}
	printf("%d\n", left);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (mat[i][j] <= left) mm[i][j] = 1;
			else mm[i][j] = 0;
		}
	}
	hungary(n, n, mm, match1, match2);
	for (int i = 0; i < n; ++i) {
		printf("%d %d\n", i + 1, match1[i] + 1);
	}
	return 0;
}
예제 #2
0
int main() {
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 1, a, b; i <= k; i ++)
		scanf("%d%d", &a, &b), map[a][b] = true;
	int ans = hungary();
	printf("%d\n", n + m - hungary());
	return 0;
}
예제 #3
0
void output()
{
    printf("%d\n",hungary() + hunterNumber);
    memset(hunterLink, 0, sizeof(hunterLink));
    for(int i=1;i<=dogNumber;++i)
    {
        if(link[i])
        {
            hunterLink[link[i]] = i;
        }
    }
    for(int i=1;i<=hunterNumber;++i)
    {
        if(i>1)
        {
            printf(" ");
        }
        hunterRoute[i].output();
        if(hunterLink[i])
        {
            printf(" ");
            dogInterest[hunterLink[i]].output();
        }
    }
    printf("\n");
}
예제 #4
0
파일: sp_684.cpp 프로젝트: liuq901/code
bool hungary(int x)
{
   f[x]=true;
   for (int i=1;i<=m;i++)
   {
      if (g[i])
         continue;
      int d=dx[x]+dy[i]-w[x][i];
      if (!d)
      {
         g[i]=true;
         if (b[i])
         {
            link[x]=i;
            next[x]=0;
            return(true);
         }
         for (int j=1;j<=n;j++)
         {
            if (f[j])
               continue;
            if (c[j][i] && hungary(j))
            {
               link[x]=i;
               next[x]=j;
               return(true);
            }
         }
      }
      else if (d<slack[i])
         slack[i]=d;
   }
   return(false);
}
예제 #5
0
파일: sp_684.cpp 프로젝트: liuq901/code
int main()
{
   bool hungary(int);
   void push(int);
   int t;
   scanf("%d",&t);
   while (t--)
   {
      scanf("%d%d",&n,&m);
      for (int i=1;i<=n;i++)
         scanf("%d",&a[i]);
      for (int i=1;i<=m;i++)
         scanf("%d",&b[i]);      
      memset(dx,-26,sizeof(dx));
      memset(dy,0,sizeof(dy));
      memset(c,0,sizeof(c));
      for (int i=1;i<=n;i++)
         for (int j=1;j<=m;j++)
         {
            scanf("%d",&w[i][j]);
            w[i][j]*=-1;
            if (w[i][j]>dx[i])
               dx[i]=w[i][j];
         }
      for (int i=1;i<=n;i++)
         while (1)
         {
            memset(slack,26,sizeof(slack));
            while (a[i])
            {
               memset(f,0,sizeof(f));
               memset(g,0,sizeof(g));
               if (hungary(i))
                  push(i);
               else
                  break;
            }
            if (!a[i])
               break;
            int d=1<<30;
            for (int i=1;i<=m;i++)
               if (!g[i] && slack[i]<d)
                  d=slack[i];
            for (int i=1;i<=n;i++)
               if (f[i])
                  dx[i]-=d;
            for (int i=1;i<=m;i++)
               if (g[i])
                  dy[i]+=d;
         }
      int ans=0;
      for (int i=1;i<=n;i++)
         for (int j=1;j<=m;j++)
            ans+=c[i][j]*w[i][j];
      printf("%d\n",-ans);
   }
   system("pause");
   return(0);
}
예제 #6
0
int main()
{
	int res;
	init();
	res = hungary();
	printf("%d\n", res);
	return 0;
}
예제 #7
0
 bool hungary(int x)
 {
 	int i;
 	for(i=1;i<=n;i++)
 		if(g[x][i]&&!visit[i]){
 			visit[i]=1;
 			if(match[i]==-1||hungary(match[i])){
 				match[i]=x;
 				return 1;
 			}
 		}
 	return 0;
 }
int hungary(int nd) {
    int i;
    x[nd] = 1;
    for(i = 1; i <= M; i++) {
        if(y[i] == 0 && fabs(W[nd][i]-lx[nd]-ly[i]) < eps) {
            y[i] = 1;
            if(my[i] == 0 || hungary(my[i])) {
                my[i] = nd;
                return 1;
            }
        }
    }
    return 0;
}
예제 #9
0
 void run()
 {
 	int i,j,a,b,ans=0;
 	memset(g,0,sizeof(g));
 	memset(match,-1,sizeof(match));
 	for(i=1;i<=k;i++){
 		scanf("%d%d",&a,&b);
 		g[a][b]=1;
 	}
 	for(i=1;i<=m;i++){
 		memset(visit,0,sizeof(visit));
 		if(hungary(i)) ans++;
 	}
 	printf("%d\n",ans);
 }
예제 #10
0
파일: 1364.c 프로젝트: Crmiv/AlgoSolution
int main()
{
    int m,n,k,i,j;

    while(scanf("%d%d%d",&m,&n,&k)==3 && m){
        memset(mp,0,sizeof(mp));
        while(k--){
            scanf("%*d%d%d",&i,&j);
            mp[i-1][j-1]=1;
        }
        printf("%d\n",hungary(m,n));
    }

    return 0;   /* NZEC */
}
예제 #11
0
파일: hdu_3605.cpp 프로젝트: keroro520/ACM
int main()
{
    int x;
    while(scanf("%d%d", &n, &m) != EOF) {
        init();
        for(int i = 0; i < n; i++) 
            for(int j = 0; j < m; j++) {
                scanf("%d", &x);
                path[i][j] = x;
            }
        for(int i = 0; i < m; i++) scanf("%d", &cap[i]);

        printf(hungary() ? "YES\n" : "NO\n");
    }
    return 0;
}
int main()
{
  init();
  
  if ( hungary() )
    work();
  else
    for( int i = 1 ; i <= on ; ++ i )
      {
        for( int j = 1 ; j <= m ; ++ j )
          printf("1");
        printf("\n");
      }
  
  return 0;
}
예제 #13
0
int main()
{
	int m, i, j;
	while(1)
	{
		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;
		memset(adj, 0, sizeof(adj));
		while(m--)
		{
			scanf("%d %d", &i, &j);
			adj[i][j] = 1;
		}
		printf("%d\n", n - hungary());
	}
	return 0;
}
예제 #14
0
int main() {
        int tot = primes(Max_M), n;
        for(scanf("%d", &_); _--; ) {
                scanf("%d", &n);
                init(n);
                for(int i = 0; i < n; ++i) {
                        scanf("%d", bn + i);
                }
                for(int i = 0, j; i < n; ++i) {
                        for(j = i+1; j < n; ++j) if(Check(bn[i], bn[j])) {
                                add(i, j);
                        }
                }
                static int cas = 0;
                printf("Case #%d: %d\n", ++cas, n - hungary(n));
        }
        return 0;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%s",map[i]+1);
		for(int j=1;j<=m;j++){
			if(map[i][j]=='.'){
				sx=i;
				sy=j;
				map[i][j]='X';
			}
		}
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			if(map[i][j]=='O'&&(i^sx^j^sy)&1){
				V[i][j].type=-1;
				if(V[i-1][j].type)
					addedge(&V[i][j],&V[i-1][j]);
				if(V[i][j-1].type)
					addedge(&V[i][j],&V[i][j-1]);
			}
			else
			if(map[i][j]=='X'&&!((i^sx^j^sy)&1)){
				V[i][j].type=1;
				if(V[i-1][j].type)
					addedge(&V[i][j],&V[i-1][j]);
				if(V[i][j-1].type)
					addedge(&V[i][j],&V[i][j-1]);
			}
		}
	hungary();
	int k; scanf("%d",&k);
	for(int t=1;t<=k;t++){
		bool tmp=win();
		scanf("%d%d",&sx,&sy);
		if(win()&&tmp)
			ans[++*ans]=t;
		scanf("%d%d",&sx,&sy);
	}
	printf("%d\n",*ans);
	for(int i=1;i<=*ans;i++) printf("%d\n",ans[i]);
}
예제 #16
0
파일: hdu_2444.cpp 프로젝트: keroro520/ACM
int main()
{
    int m, x, y;
    while(scanf("%d%d", &n, &m) != EOF) {
        init();
        for(int i = 0; i < m; i++) {    
            scanf("%d%d", &x, &y);
            add_edge(x, y);
            add_edge(y, x);
        }
        if(!dye()) {
            printf("No\n");
            continue;
        }
        
        build();
        when++;
        printf("%d\n", hungary());
    }
    return 0;
}
예제 #17
0
파일: C.cpp 프로젝트: Fengdalu/ICPC
int main() {
  int T; 
  scanf("%d", &T);
  while(T--) {
    int n, m, t;
    scanf("%d%d%d", &n, &m, &t);
    for(int i = 0; i < t; i++) scanf("%lf%lf", x + i, y + i);
    for(int i = 0; i < t; i++) {
      y[i] = y[i] - sqrt(sqr(n / 2.0) - sqr(1.0 * n / 2.0 - x[i]));
    }
    int up = (int)*std::max_element(x, x + t);
    for(int i = 0; i < up + 1; i++) g[i].clear();
    for(int i = 0; i < t; i++) {
      int u = x[i];
      // g[u].push_back(ceil(y[i]));
      g[u].push_back(floor(y[i]));
    }
    printf("%d\n", hungary(up + 1));
  }
  return 0;
}
double KM() {
    int i, j, k;
    double d;
    memset(mx, 0, sizeof(mx));
    memset(my, 0, sizeof(my));
    memset(lx, 0, sizeof(lx));
    memset(ly, 0, sizeof(ly));
    for(i = 1; i <= N; i++)
        for(j = 1, lx[i] = W[i][j]; j <= M; j++)
            lx[i] = lx[i] > W[i][j] ? lx[i] : W[i][j];
    for(i = 1; i <= N; i++) {
        while(1) {
            memset(x, 0, sizeof(x));
            memset(y, 0, sizeof(y));
            if(hungary(i))  break;
            d = 0xfffffff;
            for(j = 1; j <= N; j++) {
                if(x[j]) {
                    for(k = 1; k <= M; k++)
                        if(!y[k])
                        d = d < lx[j]+ly[k]-W[j][k] ?
                            d : lx[j]+ly[k]-W[j][k];
                }
            }
            if(d == 0xfffffff)  break;
            for(j = 1; j <= N; j++)
                if(x[j])    lx[j] -= d;
            for(j = 1; j <= M; j++)
                if(y[j])    ly[j] += d;
        }
    }
    double res = 0;
    for(i = 1; i <= M; i++) {
        if(my[i])
            res += W[my[i]][i];
    }
    return res;
}
예제 #19
0
파일: sp_373.cpp 프로젝트: liuq901/code
bool hungary(int x)
{
   if (!x)
      return(true);
   f[x]=true;
   for (int i=1;i<=n;i++)
   {
      if (g[i])
         continue;
      int t=dx[x]+dy[i]-w[x][i];
      if (!t)
      {
         g[i]=true;
         if (hungary(b[i]))
         {
            b[i]=x;
            return(true);
         }
      }
      else if (t<slack[i])
         slack[i]=t;
   }
   return(false);
}
int main ()
{
    int T, ns, np, nw;
    scanf("%d", &T);
    while(T --)
    {
        int to;
        scanf("%d %d %d", &ns, &np, &nw);
        memset(msp, 0, sizeof(msp));
        memset(msw, 0, sizeof(msw));
        memset(mpw, 0, sizeof(mpw));
        for(int i = 1; i <= ns; i ++)
        {
            int temp;
            scanf("%d", &temp);
            while(temp --)
            {
                scanf("%d", &to);
                msp[i][to] = 1;
            }
        }
        for(int i = 1; i <= ns; i ++)
        {
            int temp;
            scanf("%d", &temp);
            while(temp --)
            {
                scanf("%d", &to);
                msw[i][to] = 1;
            }
        }

        for(int i = 1; i <= np; i ++)
        {
            int temp;
            scanf("%d", &temp);
            while(temp --)
            {
                scanf("%d", &to);
                mpw[i][to] = 1;
            }
        }


        memset(g, 0, sizeof(g));
        for(int i = 1; i <= np; i ++)
        {
            for(int j = 1; j <= ns; j ++)
            {
                if(msp[j][i])
                {
                    for(int k = 1; k <= nw; k ++)
                    {
                        if(msw[j][k] && mpw[i][k])
                        {
                            g[i - 1][j - 1] = 1;
                        }
                    }
                }
            }
        }
        uN = np, vN = ns;
        printf("%d\n", hungary());
    }
    return 0;

}
예제 #21
0
파일: sp_373.cpp 프로젝트: liuq901/code
int main()
{
   bool hungary(int);
   int t;
   scanf("%d",&t);
   while (t--)
   {
      int m,p[501],a[501][501];
      scanf("%d",&n);
      for (int i=1;i<=n;i++)
         scanf("%d",&p[i]);
      memset(a,26,sizeof(a));
      scanf("%d",&m);
      for (int i=1;i<=m;i++)
      {
         int x,y;
         scanf("%d%d",&x,&y);
         a[x][y]=a[y][x]=1;
      }
      for (int i=1;i<=n;i++)
         a[i][i]=0;
      for (int k=1;k<=n;k++)
         for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
               if (a[i][k]+a[k][j]<a[i][j])
                  a[i][j]=a[i][k]+a[k][j];
      memset(dx,-26,sizeof(dx));
      memset(dy,0,sizeof(dy));
      for (int i=1;i<=n;i++)
         for (int j=1;j<=n;j++)
         {
            w[i][j]=-a[p[i]][j];
            if (w[i][j]>dx[i])
               dx[i]=w[i][j];
         }
      memset(b,0,sizeof(b));
      for (int i=1;i<=n;i++)
      {
         memset(slack,26,sizeof(slack));
         while (1)
         {
            memset(f,0,sizeof(f));
            memset(g,0,sizeof(g));
            if (hungary(i))
               break;
            int d=1<<30;
            for (int j=1;j<=n;j++)
               if (!g[j] && slack[j]<d)
                  d=slack[j];
            for (int j=1;j<=n;j++)
            {
               if (f[j])
                  dx[j]-=d;
               if (g[j])
                  dy[j]+=d;
            }
         }
      }
      int ans=0;
      for (int i=1;i<=n;i++)
         ans+=w[b[i]][i];
      printf("%d\n",-ans);
   }
   system("pause");
   return(0);
}
예제 #22
0
파일: pku2226.cpp 프로젝트: huganle/code
int Workout()
{
    Enum();
    CreateMap();    
    return hungary(Rowl, Coll, g_list);
}