# bzoj4006[JLOI2015]管道连接

http://www.lydsy.com/JudgeOnline/problem.php?id=4006

F[i][state]表示一定包含i点，至少包含关键点state的生成树的最小费用，其中state是一个二进制数。

F[i][state]=min{F[i][s]+F[i][state-s]}（其中s是state的子集）

F[i][state]=min{F[j][state]+cost}（其中i号点和j号点有边相连，费用为cost）

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ，但不适用于poj

using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
{
int res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}

const int maxN=1000;
const int maxM=3000;
const int maxP=10;
const int INF=0x3f3f3f3f;

int N,M,P;
int now,first[maxN+100];
struct Tedge{int v,next,cost;}edge[2*maxM+100];

inline void addedge(int u,int v,int cost)
{
now++;
edge[now].v=v;
edge[now].cost=cost;
edge[now].next=first[u];
first[u]=now;
}

int bit[maxN+100];
int val[maxP+10];
int F[maxN+100][two(maxP)+10];
int vis[maxN+100][two(maxP)+10];

queue<PII>Q;
inline void SPFA()
{
while(!Q.empty())
{
int u=Q.front().fi,state=Q.front().se,i,v,cost;Q.pop();
vis[u][state]=0;
for(i=first[u],v=edge[i].v,cost=edge[i].cost;i!=-1;i=edge[i].next,v=edge[i].v,cost=edge[i].cost)
if(F[u][state]+cost<F[v][state])
{
F[v][state]=F[u][state]+cost;
if(!vis[v][state])Q.push(PII(v,state)),vis[v][state]=1;
}
}
}

int DP[two(maxP)+10];

#define wei(v,k) ((v>>((k)-1))&1)
inline int check(int s)
{
int i;
re(i,1,P)if( (s&val[i])!=0 && (s&val[i])!=val[i] ) return 0;
return 1;
}

int main()
{
freopen("bzoj4006.in","r",stdin);
freopen("bzoj4006.out","w",stdout);
int i,j;
N=gint();M=gint();P=gint();
now=-1;mmst(first,-1);
re(i,1,M)
{
int u=gint(),v=gint(),cost=gint();
}
mmst(F,0x3f);
re(i,1,P)
{
int t=gint(),id=gint();
bit[id]=two(i-1);
val[t]+=bit[id];
F[id][bit[id]]=0;
}
int state,maxstate=two(P)-1;
re(state,1,maxstate)
{
re(i,1,N)
{
for(int s=(state-1)&state;s;s=(s-1)&state)
upmin(F[i][state],F[i][s]+F[i][state-s]);
if(F[i][state]!=INF)Q.push(PII(i,state)),vis[i][state]=1;
}
SPFA();
}
mmst(DP,0x3f);
re(state,1,maxstate)re(i,1,N)upmin(DP[state],F[i][state]);
re(i,1,maxstate)if(check(i))
for(j=(i-1)&i;j;j=(j-1)&i)if(check(j))
upmin(DP[i],DP[j]+DP[i-j]);
cout<<DP[maxstate]<<endl;
return 0;
}
View Code

posted @ 2015-08-26 19:56  maijing  阅读(199)  评论(0编辑  收藏