(最短路)Cow Contest

在一场涉及N头编号为1至N的牛的编程竞赛中,每头牛都有独特的技能等级。通过分析M轮两两对决的比赛结果,本文介绍了一个算法,用于确定能精确排名的牛的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2…M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

  • Line 1: A single integer representing the number of cows whose ranks can be determined

Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include<queue>
using namespace std;
const int maxn=101;
const int inf=0x3f3f3f3f;
int edge[maxn][maxn],n,t,m;
int main()
{
	int i,j,k;
	while(~scanf("%d%d",&n,&m))
	{
		int a,b;
		memset(edge,0,sizeof(edge));
		for(i=1;i<=m;i++) 
		{
			scanf("%d%d",&a,&b);
			edge[a][b]=1;
		}
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				for(k=1;k<=n;k++)
				{
					if(edge[j][i]==1&&edge[i][k]==1) edge[j][k]=1;
				}
			}
		}
		int ans=0;
		for(i=1;i<=n;i++)
		{
			int num=0;
			for(j=1;j<=n;j++)
			{
				if(edge[i][j]==1) num++;
				if(edge[j][i]==1) num++;
			}
			if(num==n-1) ans++;
		}
		printf("%d\n",ans);
	}
}
### 关于分层图短路径算法的推荐练习题 #### 背景介绍 分层图短路径问题是基于图论的一种扩展模型,通常用于处理带有额外约束条件或者动态变化的情况。例如,在某些场景下,边的权重可能随时间或其他因素改变,此时可以通过构建多层图来模拟这些变化并求解短路径。 #### 推荐题目及其解析 1. **LeetCode 787. Cheapest Flights Within K Stops** 这道题目是一个典型的分层图应用案例。给定一张航班网络图以及多允许经过 `K` 次中转的情况下,找到从起点到终点的低价格路线。此问题可以转化为在一个具有 `(n * (k+1))` 层次的分层图上运行 Dijkstra 或 Bellman-Ford 算法[^4]。 ```python import heapq def findCheapestPrice(n, flights, src, dst, k): graph = [[] for _ in range(n)] for u, v, w in flights: graph[u].append((v, w)) dist = [[float('inf')] * (k + 2) for _ in range(n)] pq = [(0, src, 0)] # cost, node, stops while pq: cost, city, stop = heapq.heappop(pq) if city == dst: return cost if stop <= k: for neighbor, price in graph[city]: new_cost = cost + price if new_cost < dist[neighbor][stop + 1]: dist[neighbor][stop + 1] = new_cost heapq.heappush(pq, (new_cost, neighbor, stop + 1)) return -1 ``` 2. **Codeforces Problem: Shortest Path with Edge Costs Changing Over Time** 此类问题涉及随着时间推移,边的成本会发生变化的情形。通过创建多个层次代表不同时间段的状态转移矩阵,并利用广度优先搜索(BFS)或改进版迪杰斯特拉算法完成计算[^5]。 3. **AtCoder Beginner Contest Example Problems on Layered Graphs** AtCoder 平台上的许多比赛也会涉及到类似的分层图建模技巧,比如某一天开放特定道路通行等问题都可以抽象成此类形式加以解答[^6]。 #### 总结说明 对于初学者而言,可以从上述提到的经典平台入手尝试解决一些基础性的分层图短路径问题;随着经验积累再逐步挑战更复杂的变种情况。值得注意的是,实际编码过程中需特别留意状态定义清晰准确以及边界条件妥善处理等方面事项[^7]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值