HDU 5772 String problem(一个诡异的建图)
来源:互联网 发布:数据库设计原则,范式 编辑:程序博客网 时间:2024/06/03 17:06
题目地址
题意:给你一个长度为n的只有0~9字符的字符串,要你挑出一些字符来组成一个新的字符串,然后挑选每个字符的代价是
然后呢,能得到的价值是(substr是新串,id[i]是原串的位置)就是求和,规则看下图。
思路:这个我还是搞得不太清楚,看到别人发的博客中放了官方题解,我就看了下,然后慢慢理解,但是建图就十分经典了,就是要理解点的划分的问题,官方题解讲的很详细了,我就不多说了。
#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#include <iomanip>#define N 6100#define M 100010 #define LL __int64#define inf 0x3f3f3f3f3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;const LL mod = 1000000007;int head[N], level[N], cur[N];int n, m, cnt;struct node { int to; LL cap;//剩余流量 int next;}edge[M];struct Dinic { void init() { memset(head, -1, sizeof(head)); cnt = 0; } void add(int u, int v, LL cap) {//有向图 edge[cnt].to = v, edge[cnt].cap = cap, edge[cnt].next = head[u], head[u] = cnt++; edge[cnt].to = u, edge[cnt].cap = 0, edge[cnt].next = head[v], head[v] = cnt++;//反向边 } bool bfs(int s, int t){ memset(level, -1, sizeof(level)); queue<int>Q; level[s] = 0; Q.push(s); while (!Q.empty()){ int u = Q.front(); Q.pop(); for (int i = head[u]; i + 1; i = edge[i].next){ int v = edge[i].to; if (edge[i].cap>0 && level[v] == -1){ level[v] = level[u] + 1; Q.push(v); } } } return level[t] != -1; } LL dfs(int u, int t, LL f){ if (u == t) return f; for (int &i = cur[u]; i + 1; i = edge[i].next){ int v = edge[i].to; if (edge[i].cap>0 && level[v] == level[u] + 1){ LL d = dfs(v, t, min(f, edge[i].cap)); if (d>0){ edge[i].cap -= d; edge[i ^ 1].cap += d; return d; } } } return 0; } LL dinic(int s, int t){ LL flow = 0; while (bfs(s, t)){ for (int i = 0; i<N; i++) cur[i] = head[i]; LL f; while ((f = dfs(s, t, inf))>0) flow += f; } return flow; }}dc;int main() { cin.sync_with_stdio(false); LL a, b, c; int numa[10], numb[10]; int mapp[110][110]; int s, t; LL sum; int ans; string str; int T, Case = 1; cin >> T; while (T--) { cin >> n; dc.init(); sum = 0; s = 0; t = n*(n - 1) / 2 + n + 10 + 1;//每个由第i个点和第j个点组成的点+n个坐标+10个字符+1 cin >> str; for (int i = 0; i < 10; i++) { cin >> numa[i] >> numb[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> mapp[i][j]; sum += mapp[i][j]; } } ans = 1; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { dc.add(s, ans, mapp[i][j] + mapp[j][i]);//价值 dc.add(ans, n*(n - 1) / 2 + i + 1, inf);//n*(n - 1) / 2 + i 第几个字符 dc.add(ans, n*(n - 1) / 2 + j + 1, inf); ans++; } } for (int i = 1; i <= n; i++) { dc.add(n*(n - 1) / 2 + i, n*(n - 1) / 2 + n + str[i - 1] - '0' + 1, inf); dc.add(n*(n - 1) / 2 + i, t, numa[str[i - 1] - '0']);//花费 } for (int i = 0; i < 10; i++) { dc.add(n*(n - 1) / 2 + n + i + 1, t, (numb[i] - numa[i])); } cout << "Case #" << Case++ << ": " << sum - dc.dinic(s, t) << endl; } return 0;}
阅读全文
0 0
- HDU 5772 String problem(一个诡异的建图)
- HDU 5772 String problem
- hdu 5772 String Problem
- HDU 5772 String problem(最大权闭合子图)
- HDU 5772 String problem(最大权闭合子图)
- 一个使用STL::string时遇到的诡异bug
- hdu 1180 诡异的楼梯(bfs)
- HDU 1180 诡异的楼梯 (BFS)
- 【HDU 1180】诡异的楼梯(BFS)
- HDU 1180 诡异的楼梯(BFS)
- hdu 1180(广搜升阶)诡异的楼梯
- hdu 诡异的楼梯(BFS)
- hdu 1180 诡异的楼梯 (BFS)
- hdu 诡异的楼梯
- HDU:诡异的楼梯
- hdu 诡异的楼梯
- 诡异的楼梯 HDU
- HDU 诡异的楼梯
- 正则表达式 字符类
- 数据结构第二周项目--C/C++语言中函数传递的三种方式
- java中 equals的用法(object 和 其他类),以及与 == 的比较
- 空间变换网络--spatial transform network
- VMware中linux,主机能ping通虚拟机,虚拟机不能ping通主机
- HDU 5772 String problem(一个诡异的建图)
- java常用的输入输出调试技巧
- 正则表达式 反义
- MyEclipse 2017 CI 7安装与激活
- [LeetCode] 258. Add Digits
- [IOS笔记]UIScrollView属性
- ES6之数组(array)
- 【Python】简单学习(一)
- IntelliJ IDEA使用之一—打开Settings