【Floyd判断连通性】
题目描述
比赛结果保证不会自相矛盾。
希望你能根据这些信息,推断出尽可能多的奶牛的编程能力排名。
FJ想知道奶牛们编程能力的具体排名,于是他找来了奶牛们所有M(1 <= M <= 4,500)轮比赛的结果,
那么她们的对决中,编号为A的奶牛总是能胜出。
如果编号为A的奶牛的编程能力强于编号为B的奶牛(1 <= A <= N; 1 <= B <= N; A != B),
整个比赛被分成了若干轮,每一轮是两头指定编号的奶牛的对决。
在赛场上,奶牛们按1..N依次编号。每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平不相上下,也就是说,奶牛们的编程能力有明确的排名。
FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:)。
输入
* 第1行: 2个用空格隔开的整数:N 和 M
* 第2..M+1行: 每行为2个用空格隔开的整数A、B,描述了参加某一轮比赛的奶牛的编号,
以及结果(编号为A,即为每行的第一个数的奶牛为胜者)
输出
* 第1行: 输出1个整数,表示排名可以确定的奶牛的数目
样例输入
5 5
4 3
4 2
3 2
1 2
2 5
样例输出
2
提示
编号为2的奶牛输给了编号为1、3、4的奶牛,也就是说她的水平比这3头奶牛都差。而编号为5的奶牛又输在了她的手下,也就是说,她的水平比编号为5的奶牛强一些。于是,编号为2的奶牛的排名必然为第4,编号为5的奶牛的水平必然最差。其他3头奶牛的排名仍无法确定。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define maxn 5003
using namespace std;
int n,m,ans=0;
bool f[102][102];
int get(){
char c;while(!isdigit(c=getchar()));
int v=c-48;while(isdigit(c=getchar()))v=v*10+c-48;
return v;
}
void init(){
memset(f,0,sizeof(f));
n=get();m=get();int x,y;
for(int i=1;i<=m;++i){f[get()][get()]=1;}
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)f[i][j]=((f[i][k]&f[k][j])|f[i][j]);
for(int i=1;i<=n;++i){
int tmp=0;
for(int j=1;j<=n;++j){
if(f[i][j]||f[j][i])++tmp;
}
if(tmp==n-1)++ans;
}
}
int main(){
init();
printf("%d\n",ans);
return 0;
}
【差分约束】
题目描述
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated. Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食。奶牛排在队伍中的顺序和它们的编号是相同的。因为奶牛相当苗条,所以可能有两头或者更多奶牛站在同一位置上。即使说,如果我们想象奶牛是站在一条数轴上的话,允许有两头或更多奶牛拥有相同的横坐标。
一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L。另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D。给出ML条关于两头奶牛间有好感的描述,再给出MD条关于两头奶牛间存有反感的描述。(1<=ML,MD<=10000,1<=L,D<=1000000)
你的工作是:如果不存在满足要求的方案,输出-1;如果1号奶牛和N号 奶牛间的距离可以任意大,输出-2;否则,计算出在满足所有要求的情况下,1号奶牛和N号奶牛间可能的最大距离。
输入
Line 1: Three space-separated integers: N, ML, and MD.
Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
输出
Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
样例输入
4 2 1
1 3 10
2 4 20
2 3 3
样例输出
27
提示
xplanation of the sample: There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
【思路】如果是第一种情况,从A向B连一条权为w的边,如果是第二种情况从B向A连一条权为-w的边,SPFA跑一遍最短路。如果无解或出现负环,则输出-1,如果无穷大,输出-2;