传送门
A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.
Output
Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.
Sample Input
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
题意:有n个点,m条普通的双向的路,w条单向的虫洞,问是否能从某个点出发走一些路回来时回到出发的点。
题解:出题人这出题人太缺德了,floyd能过,n的数目不只是500。直接floyd模板负边权,切记不能用cin输入,必定超时。
#include<iostream>
#include<cstring>
using namespace std;
int mapp[1001][1001];
int t,x,y,z,n,m,s;
int inf=1e9+10;
int dfs()
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(mapp[i][j]>mapp[i][k]+mapp[k][j])
mapp[i][j]=mapp[i][k]+mapp[k][j];
}
if(mapp[i][i]<0)
return 1;
}
}
return 0;
}
int main()
{
cin>>t;
while(t--)
{
scanf("%d%d%d",&n,&m,&s);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i==j)
mapp[i][j]=0;
else
mapp[i][j]=inf;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(z<mapp[x][y])
mapp[x][y]=mapp[y][x]=z;
}
for(int i=0;i<s;i++)
{
scanf("%d%d%d",&x,&y,&z);
mapp[x][y]=-z;
}
if(!dfs())
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
}
}