[PowerOJ1756 & network flow problem] up to 24 k re-set interval problem (cost flow)

Meaning of the questions:

 

 

 Ideas:

【problem analysis】

Maximum weight disjoint path problem can be solved with a maximum cost maximum flow.

[Modeling]

method 1

Ordering all the left end section, each of the two sections split seen vertices <ia> <ib>, T sink S to establish an additional source, and an additional vertex S '.

1, is connected to the S S 'a capacity of K, 0 cost directed edge.

2, from S 'to each <ia> connection a capacity of 1, 0 cost directed edge.

3, from each <ib> T is connected to a capacity of 1, 0 cost directed edge.

4, each vertex is connected from <ia> to <ib> a capacity of 1, the cost of the directed edge interval length.

5, for each interval i, j with all its disjoint sections each connected to the right of a capacity of 1, 0 cost directed edge.

The maximum cost of seeking maximum flow, the maximum value is the maximum cost flow length interval k may be re-set.

Method 2

The endpoints of all discrete sections, each of the end considered as a vertex, the establishment of additional source S sink T.

1, 1 is connected from the S to the vertex (leftmost vertex) a capacity of K, 0 cost directed edge.

2, from the apex 2N (rightmost vertex) to a capacity of the T-connector is K, 0 cost directed edge.

3, from vertex i to vertex i + 1 (i + 1 <= 2N), connected to an infinite capacity, cost 0 directed edge.

4, for each interval [a, B], from a corresponding vertex i to vertex j b corresponding to a capacity of a connection, the cost of the directed edge interval length.

The maximum cost of seeking maximum flow, the maximum value is the maximum cost flow length interval k may be re-set.

[Modeling Analysis]

This problem can be seen as seeking the maximum and do not want to pay the right path to K, each path for a number of disjoint interval sequence. Because it is the largest cost flow, there must be some of the interval between two paths intersect, you can see things intersection of repeated 2 times, and K is the most weight by the way

Restored K times. The simplest idea is to sort the range, the connection between an edge disjoint intervals, as each section can only be used once, so to split points, limit traffic in the points. If we are to change thinking, to network endpoints as vertices, it is the specific interval

Some edge complexity, such modeling is smaller between endpoints. The method of the number of edges is O (N ^ 2), and the number of edges method is O (N), the scale of the problem can be solved more.

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 typedef pair<int,int> PII;
  8 typedef pair<ll,ll> Pll;
  9 typedef vector<int> VI;
 10 typedef vector<PII> VII;
 11 typedef pair<ll,ll>P;
 12 #define N  50000
 13 #define M  1000000
 14 #define INF 1e9
 15 #define fi first
 16 #define se second
 17 #define MP make_pair
 18 #define pb push_back
 19 #define pi acos(-1)
 20 #define mem(a,b) memset(a,b,sizeof(a))
 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 23 #define lowbit(x) x&(-x)
 24 #define Rand (rand()*(1<<16)+rand())
 25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 26 #define ls p<<1
 27 #define rs p<<1|1
 28 
 29 const ll MOD=1e9+7,inv2=(MOD+1)/2;
 30       double eps=1e-6;
 31       int dx[4]={-1,1,0,0};
 32       int dy[4]={0,0,-1,1};
 33 
 34 struct node
 35 {
 36     int x,y;
 37 }a[N];
 38 
 39 int head[N],vet[N],nxt[N],len1[N],len2[N],dis[N],inq[N],q[N],pre[N][2],c[N],
 40     s,S,T,tot,ans1,ans2;
 41 
 42 int read()
 43 {
 44    int v=0,f=1;
 45    char c=getchar();
 46    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 47    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 48    return v*f;
 49 }
 50 
 51 void add(int a,int b,int c,int d)
 52 {
 53     nxt[++tot]=head[a];
 54     vet[tot]=b;
55      len1 [all] = c;
56      len2 [all] = d;
57      head [a] = all;
58  
59      nxt [all ++] = head [b];
60      veto [All] = a;
61      len1 [all] = 0 ;
62      len2 [all] = - d;
63      head [b] = all;
64  }
 65  
66  int spfa ()
 67  {
 68      receives (and one , s)
 69      {
 70          dis [i] = - INF;
71         inq[i]=0;
 72     }
 73     int t=0,w=1;
 74     q[1]=S; dis[S]=0; inq[S]=1;
 75     while(t<w)
 76     {
 77         t++; int u=q[t%(s+5)]; inq[u]=0;
 78         int e=head[u];
 79         while(e)
 80         {
 81             int v=vet[e];
 82             if(len1 [e] && dis [u] + len2 [e]> dis [v])
 83              {
 84                  dis [v] = dis [u] + len2 [e];
85                  pre [v] [ 0 ] = u;
86                  pre [v] [ 1 ] = e;
87                  if (! Inq [v])
 88                  {
 89                      w ++; q [w% (s + 5 )] = v; inq [v] = 1 ;
90                  }
 91              }
 92              e = nxt [e];
93          }
 94      }
 95     if(dis[T]==-INF) return 0;
 96     return 1;
 97 }
 98 
 99 void mcf()
100 {
101     int k=T,t=INF;
102     while(k!=S)
103     {
104         int e=pre[k][1];
105         t=min(t,len1[e]);
106         k=pre[k][0];
107     }
108     ans1+=t;
109     k=T;
110     while(k!=S)
111     {
112         int e=pre[k][1];
113         len1[e]-=t;
114         len1[e^1]+=t;
115         ans2+=t*len2[e];
116         k=pre[k][0];
117     }
118 }
119 
120 int main()
121 {
122     //freopen("1.in","r",stdin);
123     int n=read(),K=read();
124     int m=0;
125     rep(i,1,n)
126     {
127         a[i].x=read(),a[i].y=read();
128         c[++m]=a[i].x;
129         c[++m]=a[i].y;
130     }
131     sort(c+1,c+m+1);
132     int k=unique(c+1,c+m+1)-c-1;
133     s=k,S=++s,T=++s;
134     rep(i,1,s) head[i]=0;
135     tot=1;
136     rep(i,1,n)
137     {
138         int len=a[i].y-a[i].x;
139         a[i].x=lower_bound(c+1,c+k+1,a[i].x)-c;
140         a[i].y=lower_bound(c+1,c+k+1,a[i].y)-c;
141         add(a[i].x,a[i].y,1,len);
142     }
143     rep(i,1,k-1) add(i,i+1,K,0);
144     add(S,1,K,0);
145     add(k,T,K,0);
146     ans1=ans2=0;
147     while(spfa()) mcf();
148     printf("%d\n",ans2);
149     return 0;
150 
151 }

 

Guess you like

Origin www.cnblogs.com/myx12345/p/11766859.html