感觉这倒题没有出好,比如数据:
3
1 2 30
1 3 30
2 1 30
2 3 30
3 1 30
3 2 30
公司1到底控制了2、3没有?可以说控制了,也可以说没有,但题目意思是没有控制。
看了nocow上的解答才发现原来就是一个简单的模拟题,大家去nocow看吧:
NOCOW上关于Controlling Companies的解答
我的代码是参考usaco答案的
Code
/**//*
ID: sdjllyh1
PROG: concom
LANG: JAVA
complete date: 2008/12/20
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class concom
{
private static int n = 100;
private static int[][] owns = new int[n][n];
private static boolean[][] controls = new boolean[n][n];
private static int triples;
private static int[] triplesI;
private static int[] triplesJ;
private static int[] triplesP;
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
for (int i = 0; i < n; i++)
{
controls[i][i] = true;
}
for (int counter = 0; counter < triples; counter++)
{
int i = triplesI[counter];
int j = triplesJ[counter];
int p = triplesP[counter];
for (int k = 0; k < n; k++)
if (controls[k][i])
owns[k][j] += p;
for (int k = 0; k < n; k++)
if (owns[k][j] > 50)
addcontroller(k, j);
}
}
private static void addcontroller(int i, int j)
{
if (controls[i][j])
{
return;
}
controls[i][j] = true;
for (int k = 0; k < n; k++)
{
owns[i][k] += owns[j][k];
}
for (int k = 0; k < n; k++)
{
if (controls[k][i])
{
addcontroller(k, j);
}
}
for (int k = 0; k < n; k++)
{
if (owns[i][k] > 50)
{
addcontroller(i, k);
}
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("concom.in"));
triples = Integer.parseInt(f.readLine());
triplesI = new int[triples];
triplesJ = new int[triples];
triplesP = new int[triples];
for (int k = 0; k < triples; k++)
{
StringTokenizer st = new StringTokenizer(f.readLine());
triplesI[k] = Integer.parseInt(st.nextToken()) - 1;
triplesJ[k] = Integer.parseInt(st.nextToken()) - 1;
triplesP[k] = Integer.parseInt(st.nextToken());
}
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("concom.out")));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ((i != j) && (controls[i][j]))
{
out.println((i + 1) + " " + (j + 1));
}
}
}
out.close();
}
}
/**//*
ID: sdjllyh1
PROG: concom
LANG: JAVA
complete date: 2008/12/20
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class concom
{
private static int n = 100;
private static int[][] owns = new int[n][n];
private static boolean[][] controls = new boolean[n][n];
private static int triples;
private static int[] triplesI;
private static int[] triplesJ;
private static int[] triplesP;
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
for (int i = 0; i < n; i++)
{
controls[i][i] = true;
}
for (int counter = 0; counter < triples; counter++)
{
int i = triplesI[counter];
int j = triplesJ[counter];
int p = triplesP[counter];
for (int k = 0; k < n; k++)
if (controls[k][i])
owns[k][j] += p;
for (int k = 0; k < n; k++)
if (owns[k][j] > 50)
addcontroller(k, j);
}
}
private static void addcontroller(int i, int j)
{
if (controls[i][j])
{
return;
}
controls[i][j] = true;
for (int k = 0; k < n; k++)
{
owns[i][k] += owns[j][k];
}
for (int k = 0; k < n; k++)
{
if (controls[k][i])
{
addcontroller(k, j);
}
}
for (int k = 0; k < n; k++)
{
if (owns[i][k] > 50)
{
addcontroller(i, k);
}
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("concom.in"));
triples = Integer.parseInt(f.readLine());
triplesI = new int[triples];
triplesJ = new int[triples];
triplesP = new int[triples];
for (int k = 0; k < triples; k++)
{
StringTokenizer st = new StringTokenizer(f.readLine());
triplesI[k] = Integer.parseInt(st.nextToken()) - 1;
triplesJ[k] = Integer.parseInt(st.nextToken()) - 1;
triplesP[k] = Integer.parseInt(st.nextToken());
}
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("concom.out")));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if ((i != j) && (controls[i][j]))
{
out.println((i + 1) + " " + (j + 1));
}
}
}
out.close();
}
}