Two points + greedy--hdu 5855

Source: Internet
Author: User
Tags current time min printf

Topic Links:
http://acm.split.hdu.edu.cn/showproblem.php?pid=5855

Reference blog:
http://blog.csdn.net/queuelovestack/article/details/52222085

Analysis: Given n factories, each factory gives the construction time and cost, gives the M stores, each store gives the revenue and needs the factory supply to be profitable, the minimum time to obtain l profit (Profit = store income-the investment cost of the factory)

Exercises
The minimum time required, so we can first search time, because the time is 1~1000000000, so the binary search does not time out. Determine whether the maximum profit at this time meets the requirements after each search time.

Judging method:
We can calculate the maximum time that every store can make a profit, that is, the one that needs to be built the longest, so you can tell if the store is profitable in the current time, and then use a value to store the cost of the currently built factory. So the profit of the store = store revenue-The total cost of building the plant + the total cost of the plant has been built. Each time you select the most profitable store, update the already-established factory costs for the currently-not-selected store. Until the current cannot be selected, the maximum profit in the process is satisfied if it exceeds L, otherwise it is not satisfied.

AC Code:

/************************************************************************* > File name:1012.cpp > Author: Akira > Mail:qaq.febr2.qaq@gmail.com > Created time:2016 August 16 Tuesday 18:44 04 seconds ********************************** /#include <iostream> #include <cstdio> #include <cstring> # Include <string> #include <cstdlib> #include <algorithm> #include <queue> #include <stack > #include <map> #include <cmath> #include <vector> #include <set> #include <list> typed
EF Long Long LL;
typedef unsigned long long ULL;
typedef long double LD;

#define MST (A, B) memset (A,b,sizeof (a)) #define CLR (a) MST (a,0) #define SQR (a) ((a) * (a)) using namespace Std;

#define MAXN 100000 #define MAXM maxn*10 #define INF 1000000007 #define BUG cout<<88888888<<endl;
int T;
int n,m;

int tar;

struct Plants {int pay,t;//plant construction cost and time}p[233];        struct Shop {int pro; BusinessShop's profit int K; The factory int index[233] that the store needs;       Factory subscript int time;       Maximum time int cost for factory construction;     Total cost of the construction plant int finish;

The cost of the currently built factory is}s[233]; struct Edge {int v,next;}
EDGE[66666];
int cont;

int head[233];
    void Add (int u, int v) {edge[cont].v = v;
    Edge[cont].next = Head[u];
Head[u] = cont++;
    } void init () {cont = 0;
MST (head,-1);
} int svis[233];

int pvis[233];
    int judge (int t) {int Get =-inf;
    CLR (Svis);
    CLR (Pvis);            for (int i=1;i<=m;i++) s[i].finish = 0;                        Each store corresponds to a built factory clear 0 int Max;                        Record the maximum profit in a selected store int tmp = 0;
        Record total profit for (int i=1;i<=m;i++)//altogether M store {Max =-inf;
        int choose; for (int j=1;j<=m;j++)//Traverse every store, select the most profitable store {if (! SVIS[J] && s[j].time<=t)//If the store is not traversed and its maximum profit time does not exceed the current T time, then the judgment {I F (max<s[j].pro-s[J].cost+s[j].finish]//If the current store profit is greater than the previous selection, update the selected store {Max = S[j].pro-s[j].cos       T+s[j].finish;                                 Update profit choose = J;      Update store Subscript}} if (Max = =-inf) break;           Description No store can be selected, then jump out svis[choose] = 1;                   Mark the selected store Tmp+=max;        Update total Profit get = MAX (tmp, get); Maximum profit achieved during the update for (int j=1;j<=s[choose]. k;j++)//traverse all the factories required by the current selection store {if (!                   PVIS[S[CHOOSE].INDEX[J])//Find the factory not built {Pvis[s[choose].index[j]] = 1; Build it for (int l = head[s[choose].index[j]]; l!=-1; l = edge[l].next)//Then update the list of stores that have not been selected through the adjacency The cost of building a good factory {if (! SVIS[EDGE[L].V])//Find a store s[edge[l].v].finish + = P[S[CHOOSE].INDEX[J]].PA Y
                }}}} if (get >= tar) return get;
return 0;
    } int main () {scanf ("%d", &t);
    int tt = 1;
        while (t--) {init ();
        scanf ("%d%d%d", &n, &m, &tar);
        for (int i=1;i<=n;i++) {scanf ("%d%d", &p[i].pay, &p[i].t); } for (int i=1;i<=m;i++) {scanf ("%d%d", &s[i].pro, &s[i].
            K);
            S[i].time = S[i].cost = 0; for (int j=1;j<=s[i].
                k;j++) {scanf ("%d", &s[i].index[j]);
                Add (S[i].index[j], i);
                S[i].time = Max (S[i].time, p[s[i].index[j]].t);
            S[i].cost + = P[s[i].index[j]].pay;
        }} int l=1;
        int r=1000000000;
        int Min = INF;
        int mid;
        int ans;
            while (l<=r) {mid = (l+r)/2;
            int tmp = judge (mid);
       if (TMP)     {ans = tmp;
                min = Min (Mid, Min);
            R = mid-1;
        } else L = mid+1;
        } if (Min! = INF) printf ("Case #%d:%d%d\n", tt++, Min, ans);
    else printf ("Case #%d:impossible\n", tt++);
} return 0; }

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.