Hdu6118 Degree Bear Trading plan (2017 "Baidu Star" program design Contest-Preliminary (B))

Source: Internet
Author: User
Tags min
the trading plan of the bear Time limit:12000/6000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 374 Accepted Submission (s): 122


Problem Description the bear was involved in the business meeting of the meow-haha village, but the business conference encountered a dilemma:

Meow haha village and the surrounding villages can be seen as a total of n pieces of area, M highway composed of areas.

Due to the difference in production capacity, the section I can spend a[i] yuan to produce 1 commodities, but the maximum production of b[i].

Similarly, because of the difference in the purchasing capacity of each slice, the section I will be able to sell up to d[i] items at a price of c[i].

Because of these factors, the degree bears feel that only reasonable transfer of goods, to obtain the greatest benefit.

It is estimated that each shipment of goods 1 km, will cost 1 yuan.

So meow haha village can achieve how much profit.

The Input subject contains several sets of test data.
Each set of test data contains:
The first line of two integers n,m means meow haha village by n pieces of area, M Street.
The next n lines, four integers per line a[i],b[i],c[i],d[i] represent the region I, can be produced at the price of a[i], the maximum production b[i], to c[i] price for sale, up to sell d[i].
Next m line, three integers per line, u[i],v[i],k[i], indicating that the road connects U[i],v[i] Two slices, distance is k[i]

There may be heavy edges or self-loops.

Meet:
1<=N<=500,
1<=m<=1000,
1<=a[i],b[i],c[i],d[i],k[i]<=1000,
1<=u[i],v[i]<=n

How much money can be earned from output outputs.

Sample Input
2 1 5 5 6 1 3 5 7 7 1 2 1
Sample Output
23
Source 2017 "Baidu Star" program design contest-Preliminary round (B)
—————————————————————————————————— idea: The shortest way to calculate the profit amount between points 22. Then each point is split into 2 points x1,x2, the source point is connected with each x1[i], the capacity is b[i], the cost is 0, the meeting point is connected with each x2[i], the capacity is d[i], the cost is 0;

X1[i] and X2[J] If the profit is not negative, even the flow is infinite, the cost is the opposite number of profit, to find the maximum cost feasible flow can, solution is in the minimum cost, if the answer is the smallest, then no longer augmented direct return;


Include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <map> #include <set> #include <stack> #include <queue> #include <

vector> #include <bitset> #include <functional> using namespace std;
#define LL Long Long const int INF = 0X3F3F3F3F;
const int maxn=5005;

const int maxm=1000100;
int n, m;
int dis[505], vis[505];
int s[505], nt[2005], e[2005],l[2005];
int mp[505][505];

int a[505], b[505], c[505], d[505];
struct Edge {int u, V, C, cost, next;} EDGE[MAXM];
int SX[MAXN], CNT;

int VISIT[MAXN],DD[MAXN],PR[MAXN],AA[MAXN];
    struct Node {int id, dis;
    BOOL operator< (const node &a) const {return dis > a.dis;

}}pre,nt1;
    void Dijkstra (int ss) {priority_queue<node>q;
    memset (Vis, 0, sizeof vis);
    memset (DIS, INF, sizeof dis);
    DIS[SS] = 0;
    pre.id = ss, Pre.dis = 0;
    Q.push (pre);
  while (!q.empty ())  {pre = Q.top ();
        Q.pop ();
        Vis[pre.id] = 1;
            for (int i = s[pre.id]; ~i; i = Nt[i]) {if (Vis[e[i]]) continue;
                if (Dis[e[i]] > Dis[pre.id] + l[i]) {Dis[e[i]] = min (Dis[e[i]], dis[pre.id] + l[i]);
                Nt1.id = E[i], Nt1.dis = dis[e[i]];
            Q.push (NT1);
}}} for (int i = 1; I <= n; i++) mp[ss][i] = dis[i];
    } void Init () {cnt = 0;
memset (SX,-1, sizeof (SX));
    } void Add (int u, int v, int c, int cost) {edge[cnt].u = u;
    EDGE[CNT].V = v;
    Edge[cnt].cost = Cost;
    EDGE[CNT].C = C;
    Edge[cnt].next = Sx[u];
    Sx[u] = cnt++;
    edge[cnt].u = v;
    EDGE[CNT].V = u;
    Edge[cnt].cost =-cost;
    edge[cnt].c = 0;
    Edge[cnt].next = Sx[v];
SX[V] = cnt++;
    } bool SPFA (int ss, int ee,int &flow,int &cost) {queue<int> q;
    memset (DD, INF, sizeof DD);
    memset (visit, 0, sizeof visit); Dd[SS] = 0, Visit[ss] = 1, Pr[ss] = 0, aa[ss] = INF;
    Q.push (ss);
        while (!q.empty ()) {int u = q.front (); Q.pop ();
        Visit[u] = 0;
            for (int i = sx[u]; ~i; i = edge[i].next) {int v = EDGE[I].V;
                if (edge[i].c>0&& Dd[v]>dd[u] + edge[i].cost) {Dd[v] = Dd[u] + edge[i].cost;
                PR[V] = i;
                Aa[v] = min (Aa[u], edge[i].c);
                    if (!visit[v]) {visit[v] = 1;
                Q.push (v);

    }}}} if (dd[ee] = = INF) return 0;
      if (cost + dd[ee]*aa[ee]>cost) return 0;
    Flow + = Aa[ee];
    Cost + = Dd[ee]*aa[ee];
    int u = EE;
        while (U = ss) {edge[pr[u]].c-= Aa[ee];
        Edge[pr[u] ^ 1].c + = Aa[ee];
    u = edge[pr[u]].u;
} return 1;
    } int MCMF (int ss, int ee) {int cost = 0, flow=0; while (SPFA (SS, EE, flow, cost));
return cost;
         } int main () {while (~scanf ("%d%d", &n, &m)) {memset (S,-1, sizeof s);
        CNT = 0;
        for (int i = 1; I <= n; i++) scanf ("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
            for (int i = 1; I <= m; i++) {int u, V, K;
            scanf ("%d%d%d", &u, &v, &k);
            if (u==v) continue;
            NT[CNT] = S[u], s[u] = cnt, e[cnt] = V, l[cnt++] = k;
        NT[CNT] = S[v], s[v] = cnt, e[cnt] = u, l[cnt++] = k;
        } for (int i = 1; I <= n; i++) Dijkstra (i); for (int i = 1, i <= N; i++) for (int j = 1; J <= N; j + +) Mp[i][j] = c[j]-a[i]-mp[i]

        [j];
        Init ();
        for (int i=1;i<=n;i++) Add (0,i,b[i],0), add (i+n,2*n+1,d[i],0); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (mp[i][j]>0) Add (i,j+n,inf,-mp[i][j

        ]); printf ("%d\N ",-MCMF (0,2*n+1));
} 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.