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

最大权闭合子图:

#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. 关于在vs中添加生成命令时的注意事项

    涉及到目录最好用双引号括起来,防止在目录含有空格或文字时发生错误.例如 del "$(SolutionDir)\..\xxxxxx\xxxx\Build\*.*" /s /q xc ...

  2. python 如何读取大文件

    一般的读取文件的方法: with open(file_path, "r") as f: print f.read() 或者 with open(file_path,"r& ...

  3. STM32F103的11个定时器详解(转)

    源:STM32F103的11个定时器详解 STM32F103系列的单片机一共有11个定时器,其中:2个高级定时器4个普通定时器2个基本定时器2个看门狗定时器1个系统嘀嗒定时器 出去看门狗定时器和系统滴 ...

  4. 101个Linq例子(40-60)

    GroupBy - Simple 2 public void Linq41() { string[] words = { "blueberry", "chimpanzee ...

  5. JavaScript DOM编程艺术-学习笔记(第七章)

    第七章: 1.dom方法创建并且插入标签:(这种方法并没有改变文档的物理内容,而是在改变dom树) ①创建元素节点:createElement(); ②内部前插入:appendChild() ③创建文 ...

  6. Hiver 操作 MySQL 导致锁表

    Hadoop 搬迁到新集群后,操作主库 MySQL 导致了锁表...sad 具体锁表时间点  : 2016-1-14 14:31  到   2016-1-14 14:36 之间 在 oradba 的 ...

  7. ASCII码对应表chr(num)

    chr(9) tab空格       chr(10) 换行      chr(13) 回车        Chr(13)&chr(10) 回车换行       chr(32) 空格符      ...

  8. HDU - 2290 Find the Path(最短路)

    HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & % ...

  9. MySQL导入乱码解决

    导入时出现乱码,需要在语句中添加指定导入数据的编码格式: mysql -uroot -p database_name < database_backup.sql --default-charac ...

  10. Tomcat 配置支持APR

    对ARP支持,需要安装以下库: APR library JNI wrappers for APR used by Tomcat (libtcnative) OpenSSL libraries 其中JN ...