Roundgod and Milk Tea(多校联赛2019)

***

Roundgod and Milk Tea


Roundgod is a famous milk tea lover at Nanjing University second to none. This year, he plans to conduct a milk tea festival. There will be n n classes participating in this festival, where the i i th class has a i a_i students and will make b i b_i cups of milk tea.

Roundgod wants more students to savor milk tea, so he stipulates that every student can taste at most one cup of milk tea. Moreover, a student can’t drink a cup of milk tea made by his class. The problem is, what is the maximum number of students who can drink milk tea?
Input
The first line of input consists of a single integer T T ( 1 T 25 ) (1 \leq T \leq 25) , denoting the number of test cases.

Each test case starts with a line of a single integer n n ( 1 n 1 0 6 ) (1 \leq n \leq 10^6) , the number of classes. For the next n n lines, each containing two integers a , b a, b ( 0 a , b 1 0 9 ) (0 \leq a, b \leq 10^9) , denoting the number of students of the class and the number of cups of milk tea made by this class, respectively.

It is guaranteed that the sum of n n over all test cases does not exceed 6 × 1 0 6 6 \times 10^6 .
Output
For each test case, print the answer as a single integer in one line.
Sample Input
1
2
3 4
2 1
Sample Output
3
题目大意:
有n个班级,每班有ai个人,能够生产bi杯奶茶,每个班的学生不能喝自己班的奶茶,只能喝别的班的奶茶,问最多有多少学生能够喝到奶茶
正解: 运用Hall’s marriage theorem定理:

设二部图G=<V1,V2,E>中, 如果存在t≥1, 使得V1中每个顶点至少关联 t 条边,而V2中每个顶点至多关联t条边,则G中存在V1到V2的完备匹配.

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
ll a[1000005],b[1000005],t,n,ans,sum;
int main()
{
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
    	ans=0;
		sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld",a+i,b+i);
            ans+=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            b[i]=min(b[i],ans-a[i]);
            sum+=b[i];
        }
        printf("%lld\n",min(ans,sum));
    }
}

猜你喜欢

转载自blog.csdn.net/s260127ljy/article/details/107289275