AOJ 2266 Cache strategy (fee flow)

Source: Internet
Author: User

"Topic link" http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2266

"The main topic"

There are m barrels, n balls, the ball is numbered 1 to N, each ball has a weight of w_i.
A series of long K is given, which consists of the number of the ball. At first, the buckets were empty.
We then take a number a_j from the sequence after we go, doing the following:
If the ball a_j already exists in a bucket, then nothing is done and it costs 0 to continue.
If there is no a_j in any bucket, then find a bucket to put A_j,
If there is a ball in the bucket, take out the original ball and put the a_j in.
The cost of this operation is a_j the weight of the W_a_j, which does not matter with the barrel and the original ball.
Minimum cost?

Exercises

Let's assume we have only one bucket, so the answer is the sum of the weights,
Then we think about how to save money, the extra m-1 can let us keep a number of numbers
Until the next same serial number, save the cost of this serial number,
So, we get this bucket in this time section, the information that is occupied by this serial number
We put each bucket into a set of segments that need to be kept to the next required time section,
So we're only asking for the maximum weight that these segments can get at the most intersecting m-1 times,
Is the biggest expense we can save.

Code

#include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <    utility>using namespace Std;const int inf=0x3f3f3f3f;typedef long long ll;struct edge{int to,cap,cost,rev; Edge (int to,int cap,int Cost,int rev): To, Cap (CAP), cost (cost), rev (rev) {}};const int max_v=10010;int v,dist[max_v], prevv[max_v],preve[max_v];vector<edge> g[max_v];void add_edge (int from,int to,int cap,int cost) {G[from].push_    Back (Edge (To,cap,cost,g[to].size ())); G[to].push_back (Edge (From,0,-cost,g[from].size ()-1));}    int min_cost_flow (int s,int t,int f) {int res=0;        while (f>0) {fill (dist,dist+v,inf);        dist[s]=0;        BOOL update=1;            while (update) {update=0;                for (int v=0;v<v;v++) {if (dist[v]==inf) continue;                    for (int i=0;i<g[v].size (); i++) {Edge &e=G[v][i];                     if (e.cap>0&&dist[e.to]>dist[v]+e.cost) {   Dist[e.to]=dist[v]+e.cost;                        Prevv[e.to]=v;                        Preve[e.to]=i;                    update=1;        }}}} if (Dist[t]==inf) return-1;        int d=f;        for (int v=t;v!=s;v=prevv[v]) {d=min (D,G[PREVV[V]][PREVE[V]].CAP);        }f-=d;        RES+=D*DIST[T];            for (int v=t;v!=s;v=prevv[v]) {Edge &e=G[prevv[v]][preve[v]];            E.cap-=d;         G[v][e.rev].cap+=d; }}return Res;} void Clear () {for (int i=0;i<=v;i++) g[i].clear ();} const int Max_n=10010;int m,n,k,w[max_n],lst[max_n],a[max_n];int Solve () {for (int i=1;i<=n;i++) scanf ("%d", &w[    I]);    for (int i=1;i<=k;i++) scanf ("%d", &a[i]);    int Cnt=unique (a+1,a+k+1)-(a+1);    int res=0;    memset (lst,0,sizeof (LST)); v=cnt+1;    Clear ();        for (int i=1;i<=cnt;i++) {res+=w[a[i]];        if (Lst[a[i]]) Add_edge (Lst[a[i]],i-1,1,-w[a[i]);    Lst[a[i]]=i; }for (int i=1; i<cnt;i++) Add_edge (i,i+1,inf,0); printf ("%d\n", Res+min_cost_flow (1,cnt,m-1));}    int main () {while (~scanf ("%d%d%d", &m,&n,&k)) solve (); return 0;}

AOJ 2266 Cache strategy (charge flow)

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.