bzoj-4514 (Network Stream)

Source: Internet
Author: User

Topic Links:

4514: [Sdoi2016] Number pairing description has n kinds of numbers, the first number is AI, there are bi, the weight is CI. If two digital AI, AJ satisfies, AI is a multiple of AJ, and Ai/aj is a prime number, then these two numbers can be paired and gain CIXCJ value. A number can only participate in a single pairing and may not participate in pairing. The maximum number of pairs to be paired, provided that the sum of the values obtained is not less than 0. Input first line an integer n. The second row n integers a1, A2 、......、 an. The third row n integers b1, B2 、......、 bn.  row n integers c1, C2 、......、 CN. Output

Number of rows, maximum number of pairs

Sample Input3
2 4 8
2 200 7
-1-2 1Sample Output4HINT

N≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5

  Ideas:  This is a limit of the maximum matching problem, you can split each point into the left and right two points, the left side of the source, connected to a meeting point, the capacity of b[i], the cost is zero, about a pair of points if the relationship between the requirements, then even a pair of capacity for infinity, the cost of c[i ]*C[J] Side, the two-side symmetry, the final figure is also symmetrical, so ans/2, in the run cost flow when the SPFA along the longest Luzhan, judging the cost and 0 of the size, you can get the answer;  AC Code: 
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int maxn=210;const LL inf=1e18;int n;    LL a[maxn],b[maxn],c[maxn];struct edge{int from,to; LL Cap,flow,cost;}; int M,S,T,INQ[2*MAXN],P[2*MAXN]; LL d[2*maxn],a[2*maxn];std::vector<edge> edge;std::vector<int> g[2*maxn];inline void Add_edge (int from,    int To,ll cap,ll cost) {Edge.push_back (edge) {from,to,cap,0,cost});    Edge.push_back (Edge) {to,from,0,0,-cost});    M=edge.size ();    G[from].push_back (m-2); G[to].push_back (m-1);}    BOOL Bellmanford (LL &flow,ll &cost) {for (int i=s;i<=t;i++) D[i]=-inf;    memset (inq,0,sizeof (INQ));    D[s]=0;inq[s]=1;p[s]=0;a[s]=inf;    queue<int>qu;    Qu.push (s);        while (!qu.empty ()) {int Fr=qu.front (); Qu.pop (); inq[fr]=0;        int len=g[fr].size ();            for (int i=0;i<len;i++) {edge& e=edge[g[fr][i]]; if (e.cap>e.flow&&d[e.to]<d[fr]+e.cost) {D[E.TO]=D[FR]+e.cost;                P[e.to]=g[fr][i];                A[e.to]=min (A[fr],e.cap-e.flow);            if (!inq[e.to]) {Qu.push (e.to); inq[e.to]=1;}    }}} if (D[t]<=-inf) return false;        if (cost+d[t]*a[t]<0) {LL tep=cost/(-d[t]);        Flow+=tep;    return false;    } Flow+=a[t];    COST+=D[T]*A[T];    int u=t;        while (u!=s) {edge[p[u]].flow+=a[t];        EDGE[P[U]^1].FLOW-=A[T];    U=edge[p[u]].from; } return true;    inline ll Mincostflow () {ll flow=0;    LL cost=0;    while (Bellmanford (flow,cost)); return flow;}    inline ll Pow_mod (ll x,ll y,ll MoD) {ll s=1,base=x;        while (y) {if (y&1) S=s*base%mod;        Base=base*base%mod;    y>>=1; } return s;}    inline int isprime (LL num) {if (num<=1) return 0;    if (num==2) return 1;    if (num%2==0) return 0;        for (int i=1;i<=50;i++) {LL x=rand ()%num;        if (x==0) x + +;     if (Pow_mod (x,num-1,num)!=1) return 0; } return 1;}    int main () {//freopen ("In.txt", "R", stdin);    scanf ("%d", &n);    for (int i=1;i<=n;i++) scanf ("%lld", &a[i]);    for (int i=1;i<=n;i++) scanf ("%lld", &b[i]);    for (int i=1;i<=n;i++) scanf ("%lld", &c[i]); for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (a[i]/a[j]<=1| |            A[I]%A[J]) continue;                if (IsPrime (A[i]/a[j])) {Add_edge (j,i+n,100000000, (LL) c[i]*c[j]);            Add_edge (i,j+n,100000000, (LL) c[i]*c[j]);    }}} s=0,t=2*n+1;    for (int i=1;i<=n;i++) Add_edge (s,i,b[i],0), Add_edge (n+i,t,b[i],0);    printf ("%lld\n", Mincostflow ()/2); return 0;}

  

bzoj-4514 (Network Stream)

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.