4514: [Sdoi2016] Digital pairing | Cost Stream __ Cost stream

Source: Internet
Author: User

This problem as long as you see it is a binary graph can direct cost flow to do a.
Sdoi's examination room I was to see the second plot, why still play violence. I'm sb.

#include <algorithm> #include <iostream> #include <complex> #include <cstdlib> #include < cstring> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include <
ctime> #include <set> #include <map> #define N 222222 #define INF 1e9 using namespace std;
    int SC () {int I=0,f=1;char c=getchar (); while (c> ' 9 ' | |
    c< ' 0 ') {if (c== '-') F=-1;c=getchar ();}
    while (c>= ' 0 ' &&c<= ' 9 ') i=i*10+c-' 0 ', C=getchar ();
return i*f;
int pr[10]={2,3,5,7,11,13,17,19,23,29};
        BOOL Prime (int x) {for (int i=0;i<10;i++) {if (X==pr[i]) return 1;
        Long Long ans=1,p=pr[i],y=x-1;
        for (; y;y>>=1,p=p*p%x) if (y&1) ans=ans*p%x;
    if (ans!=1) return 0;
return 1; } struct W{int a,b,c;}
A[n];
Long Long v[n],dis[n],ans,sum;
int q[n],from[n],wh[n],mark[n],col[n];
int Head[n],nxt[n],lst[n],tot;
int head[n],nxt[n],lst[n],c[n],tot=1;
int n,m,s,t; void Insert (int x,inT Y,int A,long long B) {lst[++tot]=y;nxt[tot]=head[x];head[x]=tot;c[tot]=a;v[tot]=b;
Lst[++tot]=x;nxt[tot]=head[y];head[y]=tot;c[tot]=0;v[tot]=-b; } void INSERT (int x,int y) {lst[++tot]=y; NXT[TOT]=HEAD[X];
    Head[x]=tot; Lst[++tot]=x; Nxt[tot]=head[y];
Head[y]=tot; BOOL CMP (W a,w b) {return a.a<b.a;} void Dfs (int x) {for (int i=head[x];i;i=nxt[i]) if (!col[lst[i)]
) Col[lst[i]]=col[x]^1,dfs (Lst[i]);
    BOOL Spfa () {for (int i=1;i<=t;i++) dis[i]=-1ll<<59;
    dis[q[1]=s]=0;
        for (int l=1,r=2;l!=r;l= (l+1)%N) {int x=q[l];mark[x]=0; for (int i=head[x];i;i=nxt[i]) if (C[i]&&dis[lst[i]]<dis[x]+v[i]) {dis[l
                St[i]]=dis[x]+v[i];
                From[lst[i]]=x;
                Wh[lst[i]]=i;
            if (!mark[lst[i]]) mark[q[r]=lst[i]]=1,r= (r+1)%N;
} return dis[t]!=-1ll<<59; int main () {N=SC ();
    s=n+1,t=n+2; for (int i=1;i<=n;i++) A[I].A=SC ();
    for (int i=1;i<=n;i++) A[I].B=SC ();
    for (int i=1;i<=n;i++) A[I].C=SC ();
    Sort (a+1,a+n+1,cmp); 
                 for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) if (A[j].a%a[i].a==0&&prime (A[J].A/A[I].A))
    INSERT (I,J);
    for (int i=1;i<=n;i++) if (!col[i]) Col[i]=2,dfs (i);
        for (int i=1;i<=n;i++) if (col[i]&1) insert (i,t,a[i].b,0);
            else {insert (s,i,a[i].b,0);
        for (int j=head[i];j;j=nxt[j]) insert (I,lst[j],inf, (Long Long) a[i].c*a[lst[j]].c);
        while (SPFA ()) {int mn=1e9;
        for (int i=t;i!=s;i=from[i]) mn=min (Mn,c[wh[i]);
        for (int i=t;i!=s;i=from[i]) c[wh[i]]-=mn,c[wh[i]^1]+=mn;
            if (sum+dis[t]*mn<0) {ans-=sum/dis[t];
        Break
    else ans+=mn,sum+=dis[t]*mn;
    } cout<<ans;
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.