最大权闭合子图。建图巧妙。

最大权闭合子图:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn = + ;
const int INF = 0x7FFFFFFF;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
};
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
int n, m, s, t; void init()
{
for (int i = ; i < maxn; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
}
bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
}
int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} const int MAXM=;
int T,len,a[MAXM],b[MAXM],w[MAXM][MAXM],val[*MAXM*MAXM],ans;
char str[MAXM]; int main()
{
scanf("%d",&T); int cas=;
while(T--)
{
scanf("%d",&len); scanf("%s",str);
for(int i=;i<;i++) scanf("%d%d",&a[i],&b[i]);
for(int i=;i<len;i++) for(int j=;j<len;j++) scanf("%d",&w[i][j]); init(); ans=;
s=len*len+len+; val[s]=;
t=len*len+len++; val[t]=; for(int i=;i<len;i++) for(int j=i+;j<len;j++)
{
val[i*len+j]=w[i][j]+w[j][i];
AddEdge(i*len+j,len*len+i,INF);
AddEdge(i*len+j,len*len+j,INF);
if(val[i*len+j]>) AddEdge(s,i*len+j,val[i*len+j]), ans=ans+val[i*len+j];
else if(val[i*len+j]<) AddEdge(i*len+j,t,-val[i*len+j]);
}
for(int i=len*len;i<=len*len+len-;i++)
{
val[i]=-a[str[i-len*len]-''];
AddEdge(i,len*len+len+str[i-len*len]-'',INF);
if(val[i]>) AddEdge(s,i,val[i]), ans=ans+val[i];
else if(val[i]<) AddEdge(i,t,-val[i]);
}
for(int i=len*len+len;i<=len*len+len+-;i++)
{
val[i]=-b[i-(len*len+len)]+a[i-(len*len+len)];
if(val[i]>) AddEdge(s,i,val[i]), ans=ans+val[i];
else if(val[i]<) AddEdge(i,t,-val[i]);
}
ans=ans-dinic(s,t);
printf("Case #%d: %d\n",cas++,ans);
}
return ;
}

HDU 5772 String problem的更多相关文章

  1. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

  2. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. hdu 3374 String Problem(kmp+最小表示法)

    Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...

  5. HDU 3374 String Problem (KMP+最小最大表示)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3374 [题目大意] 给出一个字符串,求出最小和最大表示是从哪一位开始的,并且输出数量. [题解] ...

  6. hdu P3374 String Problem

    今天又在lyk大佬的博客学会了——最小表示法(异常激动发篇题解纪念一下说在前面:给luogu提个建议最小表示法的题太少了,都被hdu抢去了!!! 我们先看一下题目 看完后可以用一个字概括——蒙,两个字 ...

  7. HDU 3374 String Problem(KMP+最大(最小)表示)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...

  8. HDU - 3374:String Problem (最小表示法模板题)

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  9. hdu 3374 String Problem (kmp+最大最小表示法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小 ...

随机推荐

  1. 在mac安装numpy matplotlib scipy

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #fffff ...

  2. Java版经典兔子繁殖迭代问题——斐波那契(Fibonacci)数列

    /** * 题目: * 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子. * 假如兔子都不死,问经过month个月后,兔子的总数为多少对? */ public ...

  3. kindeditor编辑器上传图片失败 错误 405.0解决办法(亲测)

    HTTP 错误 405.0 - Method Not Allowed(省略)editor/php/upload_json.php?dir=image物理路径 http://www.gdgoga.com ...

  4. Jquery 获取上传文件大小

    <input type="file" id="file1" /> <script> var size = $("#file1& ...

  5. Myeclipse10、Maven构建Javaweb项目

    主要介绍如何使用 Myeclipse 10 构建 Maven Web 项目,关于 Maven 的介绍就略过了. 工具/原料   myeclipse apache-maven-3.1.0 方法/步骤   ...

  6. webservice底层使用Socket进行网络调用

    服务端代码(其实tomcat的原理也是这样): 客户端代码:

  7. JavaScript动态加载资源【js|css】示例代码

    在开发过程中会用到各种第三方的插件,或者自己写在单独文件中的js方法库或者css样式,在html头部总是需要写一大堆的script和link标签,如果想要自己实现动态的引入资源文件,可以使用开源的re ...

  8. Bounce 弹飞绵羊

    Bounce 弹飞绵羊 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 分块 将整个大区间分成若干块,每个点维护到下一个块需要跳的次 ...

  9. for循环---几种写法

    最常见的写法 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  10. JuneX_13

    在积分制的压力下,基本上能打的比赛都打了(除了忘了的).打了这么多比赛(其实也不多),发现有相当一部分题目考察的还是挺基础的内容,像搜索,DP,树,图,然而做的并不好.要么直接不会敲,要么调试大半天, ...