[Nonsense] HDU 3101 the heart of the country

Source: Internet
Author: User

During the competition, the primary node did not understand

There are n cities, each city has Val soldiers, and there are several connections

When an enemy attacks a city, the total number of soldiers in the city and connected cities is greater than K.

No more than K. The city is attacked, and soldiers are captured, so they cannot support other cities.

Find the number of cities not attacked and the total number of soldiers in the last region.


Idea: first calculate the total number of soldiers that can be supported by this point

Then, the total number of soldiers in the connected city is less than K.

Until the remaining points are greater than K

#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <cmath>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#include <time.h>;#define cler(arr, val)    memset(arr, val, sizeof(arr))#define FOR(i,a,b)  for(int i=a;i<=b;i++)#define IN   freopen ("in.txt" , "r" , stdin);#define OUT  freopen ("out.txt" , "w" , stdout);typedef long long  LL;const int MAXN = 1032;const int MAXM = 444;const int INF = 0x3f3f3f3f;const int mod = 1000000007;vector<int>G[MAXN];int val[MAXN],vis[MAXN],ans,num,orz[MAXN],man;void init(){    for(int i=0; i<MAXN; i++)        G[i].clear();    cler(val,0);    cler(vis,0);}int main(){    int n,k;    //IN;    while(scanf("%d%d",&n,&k),n+k)    {        init();        for(int i=0; i<n; i++)        {            int a,m;            scanf("%d%d",&val[i],&m);            for(int j=0; j<m; j++)            {                scanf("%d",&a);                G[i].push_back(a);            }        }        for(int i=0; i<n; i++)        {            orz[i]=val[i];            for(int j=0; j<G[i].size(); j++)                orz[i]+=val[G[i][j]];        }        ans=0,num=0;        queue<int>q;        for(int i=0; i<n; i++)            if(orz[i]<k)                q.push(i),vis[i]=1;        while(!q.empty())        {            int i=q.front();            q.pop();            for(int j=0; j<G[i].size(); j++)            {                orz[G[i][j]]-=val[i];                if(orz[G[i][j]]<k&&!vis[G[i][j]])                    q.push(G[i][j]),vis[G[i][j]]=1;            }        }        for(int i=0; i<n; i++)            if(!vis[i])                num++,ans+=val[i];        printf("%d %d\n",num,ans);    }    return 0;}


[Nonsense] HDU 3101 the heart of the country

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.