标签:ring 补丁 接下来 包含 选择 pac 空格 scan stream
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
const int maxn=5000;
namespace Dinic {
struct Edge{
int u,v,cap,flow;
Edge(int u,int v,int cap,int flow):u(u),v(v),cap(cap),flow(flow){
}
};
std::vector<Edge> edges;
std::vector<int> G[maxn];
int n,s,t;
int d[maxn];
bool vis[maxn];
int cur[maxn];
void addEdge(int u,int v,int cap) {
edges.push_back(Edge(u,v,cap,0));
edges.push_back(Edge(v,u,0,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
bool BFS() {
std::queue<int> Q;
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()) {
int u=Q.front();
Q.pop();
for(register int i=0;i<G[u].size();++i) {
Edge& e=edges[G[u][i]];
if(!vis[e.v]&&e.cap>e.flow) {
d[e.v]=d[u]+1;
Q.push(e.v);
vis[e.v]=1;
}
}
}
return vis[t];
}
int DFS(int u,int a) {
if(u==t||a==0) return a;
int flow=0,f;
for(register int &i=cur[u];i<G[u].size();++i) {
Edge& e=edges[G[u][i]];
if(d[e.v]==d[u]+1&&(f=DFS(e.v,std::min(a,e.cap-e.flow)))>0) {
e.flow+=f;
a-=f;
edges[G[u][i]^1].flow-=f;
flow+=f;
if(a==0) break;
}
}
return flow;
}
int maxFlow(int s,int t) {
Dinic::s=s;
Dinic::t=t;
int ans=0;
while(BFS()) {
memset(cur,0,sizeof(cur));
ans+=DFS(s,t);
}
return ans;
}
}
int n,m,a,b;
int main() {
// freopen("test.txt","r",stdin);
scanf("%d%d",&m,&n);
scanf("%d%d",&a,&b);
while(a!=-1&&b!=-1) {
Dinic::addEdge(a,b,1);
scanf("%d%d",&a,&b);
}
for(register int i=1;i<=m;i++) {
Dinic::addEdge(0,i,1);
}
for(register int i=1;i<=n;i++) {
Dinic::addEdge(i+m,n+m+1,1);
}
int mx=Dinic::maxFlow(0,n+m+1);
if(mx==0) {
puts("No Solution!");
return 0;
}
printf("%d\n",mx);
for(register int i=1;i<=m;i++) {
for(register int j=0;j<Dinic::G[i].size();++j) {
Dinic::Edge& e = Dinic::edges[Dinic::G[i][j]];
if(e.flow==1){
printf("%d %d\n",i,e.v);
break;
}
}
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<ctime>
#define LL long long
using namespace std;
const int N=21,M=101,inf=1e9;
int n,m,val[M],b1[M],b2[M],f1[M],f2[M],dis[1<<N],q[1<<(N+1)],h,t;
bool inq[1<<N];
char s[N];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void init()
{
n=read(); m=read();
for (int i=1;i<=m;i++)
{
val[i]=read();
scanf("%s",&s);
for (int j=0;j<n;j++)
if (s[j]=='+') b1[i]|=(1<<j);
else if (s[j]=='-') b2[i]|=(1<<j);
scanf("%s",&s);
for (int j=0;j<n;j++)
if (s[j]=='+') f2[i]|=(1<<j);
else if (s[j]=='-') f1[i]|=(1<<j);
}
}
void spfa()
{
int ljj=(1<<n)-1;
for (int i=0;i<ljj;i++) dis[i]=inf;
h=1; t=1;
q[1]=ljj; dis[ljj]=0;
while (h<=t)
{
int x=q[h++]; inq[x]=0;
for (int i=1;i<=m;i++)
if (!(x&b2[i])&&(x|b1[i])==x)
{
int y=x&(~f1[i])|f2[i];
if (dis[x]+val[i]<dis[y])
{
dis[y]=dis[x]+val[i];
if (!inq[y]) q[++t]=y,inq[y]=1;
}
}
}
}
int main()
{
init();
spfa();
if (dis[0]==inf) printf("0");
else printf("%d",dis[0]);
return 0;
}
标签:ring 补丁 接下来 包含 选择 pac 空格 scan stream
原文地址:https://www.cnblogs.com/Syameimaru/p/10123428.html