1076. Trash (km algorithm binary best Perfect Match)

Source: Internet
Author: User

1076. Trashtime limit:1.0 Second
Memory limit:64 MB
You were just hired as CEO of the local junkyard. One of your jobs is dealing with the incoming trash and sorting it for recycling. The trash comes every day in NContainers and each of these containers contains certain amount of each of the NTypes of trash. Given The amount of trash in the containers find the optimal a-to sort the trash. Sorting the trash means putting every type of trash in separate container. Each of the given containers have infinite capacity. The effort for moving one unit of trash from container ITo Jis 1 if IJOtherwise it is 0.You and minimize the total effort. Inputthe First line contains the number N(1≤ N≤150), the rest of the input contains the descriptions of the containers. The (1 + I)-th line contains the description of the I-th container The J-th Amount (0≤amount≤100) on this line denotes the amount of the J-th type of trash in the I-th container. Outputyou should write the minimal effort that was required for sorting the trash. Sample
input Output
462 41 86 9473 58 11 1269 93 89 8881 40 69 13
650
problem Author:Jivko Ganev

Tags: graph theory ()


Oh, God! Forgive elder brother this time set oneself did not understand template bar ~ Feel good uncomfortable ~, time is urgent! ~ It's the only way. Test the template is right. 1a, and then I will get it to my small template Library ~, indeed some things really difficult to understand, but should use this time to do something more meaningful! Set the black box code for the first time. hehe ~


#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace std;const int inf=10000;  int w[inf][inf],n;int Lx[inf],ly[inf];//int Left[inf];    BOOL S[inf],t[inf];//int sum[inf];int matrix[inf][inf];bool match (int i) {s[i]=true; for (int j=1; j<=n; J + +) if (lx[i]+ly[j]==w[i][j]&&!            T[j]) {t[j]=true; if (! left[j]| |                Match (Left[j])) {left[j]=i;            return true; }} return false;}    void Update () {int a=1<<30; for (int i=1, i<=n; i++) if (S[i]) for (int j=1; j<=n; J + +) if (!    T[J]) a=min (A,lx[i]+ly[j]-w[i][j]);        for (int i=1; i<=n; i++) {if (s[i]) lx[i]-=a;    if (T[i]) ly[i]+=a;    }}int KM () {for (int i=1; i<=n; i++) {left[i]=lx[i]=ly[i]=0;    for (int j=1; j<=n; j + +) Lx[i]=max (Lx[i],w[i][j]);        } for (int i=1, i<=n; i++) {for (;;)            {for (int j=1; j<=n; j + +) s[j]=t[j]=0;            if (Match (i)) break;        else update ();    }} int sum = 0;    for (int i = 1; I <= n; i + +) if (Match[i] > 0) sum + = W[left[i]][i]; return sum;}        int main () {while (cin>>n) {memset (sum,0,sizeof (sum)); for (int. I=1; i<=n; i++) for (int j=1; j<=n; J + +) {scanf ("%d", &matrix[i][j])                ;            SUM[J]+=MATRIX[I][J];        } for (int i=1, i<=n; i++) for (int j=1; j<=n; j + +) w[i][j]=-(Sum[j]-matrix[i][j]);    COUT&LT;&LT;-KM () <<endl; } return 0;}


1076. Trash (km algorithm binary best Perfect Match)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.