zoukankan      html  css  js  c++  java
  • [洛谷P1640][SCOI2010]连续攻击游戏

    题目大意:有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示。每种装备最多只能使用一次,且只能使用其中一种属性。装备所使用的属性值必须从1开始连续。问最多能攻击多少次?

    题解:每个装备从属性像编号连边,匈牙利算法,跑出匹配,若一个匹配不了就结束

    卡点:

    C++ Code:

    #include<cstdio>
    using namespace std;
    int n,idx,ans;
    int head[10010],cnt;
    struct Edge{
    	int to,nxt;
    }e[1000010<<1];
    int v[10010],res[1000010];
    void add(int a,int b){
    	e[++cnt]=(Edge){b,head[a]};head[a]=cnt;
    }
    bool dfs(int x){
    	if (v[x]==idx)return false;
    	v[x]=idx;
    	for (int i=head[x];i;i=e[i].nxt){
    		int to=e[i].to;
    		if (!res[to]||dfs(res[to])){
    			res[to]=x;return true;
    		}
    	}
    	return false;
    }
    int main(){
    	scanf("%d",&n);
    	for (int i=1;i<=n;i++){
    		int x,y;
    		scanf("%d%d",&x,&y);
    		add(x,i),add(y,i);
    	}
    	for (int i=1;i<=10000;i++){
    		idx++;
    		if (dfs(i))ans++;else break;
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    HDU What Are You Talking About
    谷歌面试题
    POJ 2299 UltraQuickSort
    单链表排序
    HDU Hat’s Words
    C++ const关键字
    求二叉树任意两点间的距离
    HDU Phone List
    POJ 2352 Stars
    C++ volatile关键字
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9256966.html
Copyright © 2011-2022 走看看