bzoj 3876: [Ahoi2014]支线剧情

 就是加一个1的下界就好了。

 1 #include<bits/stdc++.h>
 2 #define N 100005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define ls tr[x][0]
 6 #define rs tr[x][1]
 7 using namespace std;
 8 inline int ra()
 9 {
10     int x=0,f=1; char ch=getchar();
11     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
12     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
13     return x*f;
14 }
15 struct node{
16     int c,v,from,to,next;
17 }e[N];
18 int tot=1,ans,from[N],n,m,S,T,head[N],cnt,dis[N],q[N<<1];
19 bool inq[N];
20 void ine(int x, int y, int v, int c)
21 {
22     e[++tot].to=y; e[tot].next=head[x]; head[x]=tot;
23     e[tot].v=v; e[tot].c=c; e[tot].from=x;
24 }
25 void insert(int x, int y, int c, int v)
26 {
27     ine(x,y,v,c); ine(y,x,-v,0);
28 }
29 bool spfa()
30 {
31     for (int i=1; i<=n+2; i++) dis[i]=inf;
32     int l=0,r=1; q[0]=S; dis[S]=0; inq[S]=1;
33     while (l<r)
34     {
35         int x=q[l++];
36         for (int i=head[x];i;i=e[i].next)
37             if (dis[e[i].to]>dis[x]+e[i].v && e[i].c)
38             {
39                 dis[e[i].to]=dis[x]+e[i].v;
40                 from[e[i].to]=i;
41                 if (!inq[e[i].to])
42                 {
43                     inq[e[i].to]=1;
44                     q[r++]=e[i].to;
45                 }
46             }
47         inq[x]=0;
48     }
49     if (dis[T]==inf) return 0;
50     return 1;
51 }
52 void mcf()
53 {
54     int x=inf;
55     for (int i=from[T];i;i=from[e[i].from]) x=min(x,e[i].c);
56     for (int i=from[T];i;i=from[e[i].from]) ans+=x*e[i].v,e[i].c-=x,e[i^1].c+=x;
57 }
58 void fyl()
59 {
60     while (spfa()) mcf();
61 }
62 int main()
63 {
64     n=ra();
65     S=n+1; T=n+2;
66     for (int i=1; i<=n; i++)
67     {
68         int cnt=ra();
69         insert(i,T,cnt,0);
70         if (i!=1) insert(i,1,inf,0);
71         for (int j=1; j<=cnt; j++)
72         {
73             int x=ra(),v=ra();
74             insert(S,x,1,v);
75             insert(i,x,inf,v);
76         }
77     }
78     fyl();
79     cout<<ans;
80     return 0;
81 }

 

posted @ 2017-02-12 22:48  ws_ccd  阅读(106)  评论(0编辑  收藏  举报