题意:给出若干个句子,每个句子包含多个单词。确定第一句是英文,第二句是法文。后面的句子两者都有可能。两个语种会有重复单词。

现在要找出一种分配方法(给每个句子指定其文种),使得既是英文也是法文的单词数量最少。

分析:网络流的最小割。

建图方法如下,每个句子一个点。每个单词一个点。句子向其所属的单词连双向无穷流量边。把第一个句子作为起点,第二句作为终点。

现在我们要割掉一些单词,使得起点无法到达终点。

图的意义是这样的。如果我们能找到一条从起点到达终点的通路,那么中间一定有一个过程是从一个英文句子跳到一个单词,然后跳到一个法文句子。这就说明该单词既是英文又是法文。

而如果找不到通路,把所有能到达的句子点归为英文,其余的归为法文。这样就成功地完成了划分任务,而没有同属于两个语言的单词。

所以,割掉单词使得图没有通路就是一种划分的充要条件。

割点的方法就是拆点,每个单词拆成两点,一个负责入边,一个负责出边。中间加一条流量为1的边。

本题还有一个难点就是输入,每个句子要自己根据空格划分成单词。

stringstream可以将字符串作为输入流,从中读入内容。用stringstream sin(inputstring); 之后读入方法与cin一样。需要包含sstream头文件。

char*转化成string可以直接用等号赋值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <sstream>
using namespace std; #define D(x) const int MAX_N = ;
const int MAX_DIC = ;
const int MAX_WORD_NUM = ; const int MAX_NODE_NUM = (MAX_N * + ) * + MAX_N;
const int MAX_EDGE_NUM = (int)(1e6);
#define INF 0x3f3f3f3f struct Edge
{
int next, v, f;
Edge()
{}
Edge(int next, int v, int f):next(next), v(v), f(f)
{}
} edge[MAX_EDGE_NUM * ]; int head[MAX_NODE_NUM];
int q[MAX_NODE_NUM];
bool vis[MAX_NODE_NUM];
int cur[MAX_NODE_NUM];
int dep[MAX_NODE_NUM];
int edge_cnt;
int path[MAX_NODE_NUM];
int front, rear, q_size; void add_edge(int u, int v, int f)
{
edge[edge_cnt] = Edge(head[u], v, f);
head[u] = edge_cnt++;
edge[edge_cnt] = Edge(head[v], u, );
head[v] = edge_cnt++;
} void init()
{
edge_cnt = ;
memset(head, -, sizeof(head));
} void q_init(int size)
{
front = ;
rear = ;
q_size = size;
} void q_push(int a)
{
q[rear++] = a;
rear %= q_size;
} int q_pop()
{
int ret = q[front++];
front %= q_size;
return ret;
} void bfs(int s, int t)
{
memset(vis, , sizeof(vis));
memset(dep, -, sizeof(dep));
q_init(MAX_NODE_NUM);
q_push(s);
vis[s] = true;
dep[s] = ;
while (front != rear && !vis[t])
{
int u = q_pop();
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (!vis[v] && edge[i].f > )
{
q_push(v);
vis[v] = true;
dep[v] = dep[u] + ;
}
}
}
} int add_flow(int path[], int &path_n)
{
int min_edge = -, delta = INF;
for (int i = ; i < path_n; ++i)
{
if (edge[path[i]].f < delta)
{
delta = edge[path[i]].f;
min_edge = i;
}
}
for (int i = ; i < path_n; ++i)
{
edge[path[i]].f -= delta;
edge[path[i] ^ ].f += delta;
}
path_n = min_edge;
return delta;
} int last_node(int path[], int path_n, int s)
{
if (path_n)
return edge[path[path_n - ]].v;
return s;
} int find_next(int start)
{
for (int e = cur[start]; ~e; e = edge[e].next)
if (edge[e].f && dep[start] + == dep[edge[e].v])
return e;
return -;
} int dfs(int s, int t)
{
int ret = ;
int path_n = ;
int x = s;
memcpy(cur, head, sizeof(cur));
while (true)
{
if (x == t)
{
ret += add_flow(path, path_n);
x = last_node(path, path_n, s);
}
int next_edge = find_next(x);
cur[x] = next_edge;
if (next_edge == -)
{
if (path_n == )
break;
dep[x] = -;
--path_n;
x = last_node(path, path_n, s);
continue;
}
path[path_n++] = next_edge;
x = edge[next_edge].v;
}
return ret;
} int dinic(int s, int t)
{
int ret = ;
while (true)
{
bfs(s, t);
if (dep[t] == -)
return ret;
ret += dfs(s, t);
}
return -;
} int n;
map<string, int> dictionary;
vector<string> word[MAX_N]; void input()
{
dictionary.clear();
scanf("%d", &n);
getchar();
for (int i = ; i < n; i++)
{
word[i].clear();
char st[MAX_DIC * MAX_WORD_NUM];
fgets(st, MAX_DIC * MAX_WORD_NUM, stdin);
string s = st;
stringstream sin(s);
while (sin >> s)
{
if (dictionary.find(s) == dictionary.end())
dictionary[s] = dictionary.size() - ;
word[i].push_back(s);
}
}
} int work()
{
init();
for (int i = ; i < (int)dictionary.size(); i++)
{
int id1 = i * + n;
int id2 = id1 + ;
add_edge(id1, id2, );
}
for (int i = ; i < n; i++)
{
for (int j = ; j < (int)word[i].size(); j++)
{
int id = dictionary[word[i][j]];
int id1 = id * + n;
int id2 = id1 + ;
add_edge(i, id1, INF);
add_edge(id2, i, INF);
}
}
return dinic(, );
} int main()
{
int t;
scanf("%d", &t);
int case_num = ;
while (t--)
{
case_num++;
printf("Case #%d: ", case_num);
input();
printf("%d\n", work());
bfs(, );
}
return ;
}

Google Code Jam 2015 R2 C的更多相关文章

  1. Google Code Jam 2015 R1C B

    题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...

  2. Google Code Jam 2015 Round1A 题解

    快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...

  3. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  4. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  5. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  6. [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

  7. [Google Code Jam (Qualification Round 2014) ] A. Magic Trick

    Problem A. Magic Trick Small input6 points You have solved this input set.   Note: To advance to the ...

  8. [C++]Store Credit——Google Code Jam Qualification Round Africa 2010

    Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...

  9. [C++]Saving the Universe——Google Code Jam Qualification Round 2008

    Google Code Jam 2008 资格赛的第一题:Saving the Universe. 问题描述如下: Problem The urban legend goes that if you ...

随机推荐

  1. mapreduce 自定义数据类型的简单的应用

    本文以手机流量统计为例: 日志中包含下面字段 现在需要统计手机的上行数据包,下行数据包,上行总流量,下行总流量. 分析:可以以手机号为key 以上4个字段为value传传递数据. 这样则需要自己定义一 ...

  2. root用户自动登录

    编辑文件: /etc/gdm/custom.conf的内容: 1 # GDM configuration storage      2       3 [daemon]      4 #GtkModu ...

  3. C/C++使用HTTP协议上传

    上传文件: http://zengrong.net/post/2088.htm #include <stdio.h> #include <string.h> #include ...

  4. 阿里云9折推荐码:0LGVW2

    阿里云9折推荐码:0LGVW2,第一次购买云服务器或云数据库可享受原价9折优惠.

  5. MVC中你必须知道的13个扩展点

    MVC中你必须知道的13个扩展点 pasting 转:http://www.cnblogs.com/kirinboy/archive/2009/06/01/13-asp-net-mvc-extensi ...

  6. netstat命令的常见用法(转)

    netstat 的10个基本用法 Netstat 简介 Netstat 是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监 ...

  7. 微信连wifi正式全量对外开放申请 升级智能服务

    之前我们提到过微信公众平台"微信连Wi-Fi"功能来了,昨日,微信连Wi-Fi自助申请入口正式全量对外开放(独立申请入口https://wifi.weixin.qq.com/),意 ...

  8. Swift3.0P1 语法指南——集合类型

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  9. debian8 配置使用 nfs

    操作过的步骤: 1.dpkg-reconfigre rpcbind. 2.在终端上退出要挂载的目录. 错误:mount -t nfs 172.16.0.121:/home/junda /mnt,出现以 ...

  10. javascript高级程序设计---Event对象三

    进度事件 进度事件用来描述一个事件进展的过程,比如XMLHttpRequest对象发出的HTTP请求的过程.<img>.<audio>.<video>.<st ...