C++——【USACO 5.4.3】——Telecowmunication

来源:互联网 发布:类似ostagram的软件 编辑:程序博客网 时间:2024/06/06 11:39

Telecowmunication

Farmer John's cows like to keep in touch via email so they have created a network of cowputers so that they can intercowmunicate. These machines route email so that if there exists a sequence of c cowputers a1, a2, ..., a(c) such that a1 is connected to a2, a2 is connected to a3, and so on then a1 and a(c) can send email to one another.

Unfortunately, a cow will occasionally step on a cowputer or Farmer John will drive over it, and the machine will stop working. This means that the cowputer can no longer route email, so connections to and from that cowputer are no longer usable.

Two cows are pondering the minimum number of these accidents that can occur before they can no longer use their two favorite cowputers to send email to each other. Write a program to calculate this minimal value for them, and to calculate a set of machines that corresponds to this minimum.

For example the network:

               1              /               3 - 2
shows 3 cowputers connected with 2 lines. We want to send messages between cowputers 1 and 2. Direct lines connect 1-3 and 2-3. If cowputer 3 is down, them there is no way to get a message from 1 to 2.

PROGRAM NAME: telecow

INPUT FORMAT

Line 1Four space-separated integers: N, M, c1, and c2. N is the number of computers (1 <= N <= 100), which are numbered 1..N. M is the number of connections between pairs of cowputers (1 <= M <= 600). The last two numbers, c1 and c2, are the id numbers of the cowputers that the communicating cows are using. Each connection is unique and bidirectional (if c1 is connected to c2, then c2 is connected to c1). There can be at most one wire between any two given cowputers. Computers c1 and c2 will not have a direct connection.Lines 2..M+1The subsequent M lines contain pairs of cowputers id numbers that have connections between them.

SAMPLE INPUT (file telecow.in)

3 2 1 21 32 3

OUTPUT FORMAT

Generate two lines of output. The first line is the minimum number of (well-chosen) cowputers that can be down before terminals c1 & c2 are no longer connected. The second line is a minimal-length sorted list of cowputers that will cause c1 & c2 to no longer be connected. Note that neither c1 nor c2 can go down. In case of ties, the program should output the set of computers that, if interpreted as a base N number, is the smallest one.

SAMPLE OUTPUT (file telecow.out)

13


Telecowmunication
农民约翰的牛喜欢通过电子邮件保持联系,所以他们建立了一个牛的网络,这样他们就可以互相交流了。这些机器会发送电子邮件,这样如果存在c台电脑 a1,a2,……ac,a1连接到a2,a2连接到a3,因此a1和ac可以互相发送电子邮件。
不幸的是,一头牛偶尔会踩到网线或被农夫约翰的车碾过,机器就会停止工作。这就意味着,cowputer不能再发送电子邮件了,所以cowputer的连接已经不再可用了。
两头牛正在考虑这些事故的最小数量,在它们不能再用它们最喜欢的两个牛儿互相发电子邮件之前。编写一个程序来计算它们的最小值,并计算一组对应于此最小值的机器。
例如网络:
   1
  /
3 - 2
我们想在cowputer 1和2之间发送消息。直线连接3-1和2-3。如果cowputer 3已经关闭,则无法从1、2获得消息。
项目名称:telecow
输入格式
第一行4个空格分隔的整数:N、M、c1和c2。N是计算机的个数(1<=N<=100),它被编号为1。M是一对cowputer的连接数(1<=M<=600)。最后两个数字,c1和c2,是牛使用的id号。每个连接都是惟一的和双向的(如果c1连接到c2,那么c2连接到c1)。在任何两个给定的“牛”之间,最多可以有一根电线。计算机c1和c2不会有直接连接。
行2 . .M+1后面的M行包含一对牛的id号,它们之间有连接。
示例输入(文件telecow.in)
3 2 1 2
1 3
2 3
输出格式
生成两行输出。第一行是在终端c1和c2不再连接之前可以关闭的最小数量的(精心选择的)的cowputer。第二行是一个最小长度排序的cowputer列表,它将导致c1和c2不再连接。注意,c1和c2都不能下降。如果有关联,程序应该输出一组字典序最小计算机。
样例输出(文件telecow.out)
1
3




——卡了N天,我选择狗带,,


/*ID : mcdonne1LANG : C++TASK : telecow*/#include <iostream>#include <fstream>#include <algorithm>#include <cstring>using namespace std;int n, m, x, y, c1, c2, tot, flag;int graph[210][210], go[210][210], al[210][210];int flow[210], d[210];const int inf = 1 << 30;int mp (int u, int fw) {if (u + 1 == c2) return fw;int tot, ans = 0;for (int i = 1; i <= n << 1; i++)if (go[u][i] and d[u] == d[i] + 1) {tot = mp (i, min (go[u][i], fw - ans));ans += tot;go[i][u] += tot;go[u][i] -= tot;if (ans == fw) return ans;}if (d[c1] >= n << 1) return ans;if (!--flow[d[u]]) d[c1] = n << 1;++flow[++d[u]];return ans;}inline int mf () {register int tot = 0;memcpy (go, graph, sizeof(graph));memset (flow, 0, sizeof(flow));memset (d, 0, sizeof(d));flow[0] = n << 2;while (d[c1] < n << 1) tot += mp(c1, inf);return tot;}int main () {ifstream fin("telecow.in", ios::in);ofstream fout("telecow.out", ios::out);fin>>n>>m>>c1>>c2;c1 <<= 1;c2 <<= 1;for (register int i = 1; i <= n; i++) graph[(i << 1) - 1][i << 1] = 1;for (register int i = 1; i <= m; i++) {fin>>x>>y;graph[x << 1][(y << 1) - 1] = graph[y << 1][(x << 1) - 1] = inf;}tot = mf();fout<<tot<<'\n';memcpy (al, go, sizeof(go));for (register int i = 1; i <= n; i++) if (!al[(i << 1) - 1][i << 1] and i != c1 >> 1 and i != c2 >> 1) {graph[(i << 1) - 1][i << 1] = 0;if (mf() + 1 == tot) {memcpy (al, go, sizeof(go));--tot;flag ? fout<<' '<<i : fout<<i, flag = 1;} else graph[(i << 1) - 1][i << 1] = 1;}fout<<'\n';fin.close();fout.close();return 0;}