USACO奶牛回家

题目描述
现在是晚餐时间,而母牛们在外面分散的牧场中。
农民约翰按响了电铃,所以她们开始向谷仓走去。
你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只速度最快的母牛)。
在挤奶的时候(晚餐前),每只母牛都在她自己的牧场上,一些牧场上可能没有母牛。
每个牧场由一条条道路和一个或多个牧场连接(可能包括自己)。
有时,两个牧场(可能是自我相同的)之间会有超过一条道路相连。
至少有一个牧场和谷仓之间有道路连接。
因此,所有的母牛最后都能到达谷仓,并且母牛总是走最短的 路径。
当然,母牛能向着任意一方向前进,并且她们以相同的速度前进。
牧场被标记为’a’…‘z’和’A’…‘Y’,在用大写字母表示的牧场中有一只母牛,小写字母中则没有。
谷仓的标记是’Z’,注意没有母牛在谷仓中。

输入
第 1 行: 整数 P(1<= P<=10000),表示连接牧场(谷仓)的道路的数目。
第 2 …P+1行: 用空格分开的两个字母和一个整数:
被道路连接牧场的标记和道路的长度(1<=长度<=1000)。

输出
单独的一行包含二个项目:
最先到达谷仓的母牛所在的牧场的标记,和这只母牛走过的路径的长度。

样例输入

5
A d 6
B d 3
C e 9
d Z 8
e Z 3 

样例输出

B 11

题意:
求出最先到达谷仓Z奶牛并写出最短路径的长度;
在写的时候需要注意的是小写z并不是第56个节点,而是第58个原因是z-A+1==58;
其他的就按照最短路的模板来写就可以;

#include <stdio.h>
#include <string.h>
int dp[5001][5001];
int dis[10000],book[10000];
int inf=99999999;
int main()
{
    int n,m,i,j,k,t,w,min,a,b;
    char u,v;
    while(scanf("%d", &n)==1)
    {
    for(i=1; i<=58; i++)
      for(j=1; j<=58; j++)
         if(i==j) dp[i][j]=0;
         else  dp[i][j]=inf;
    for(i=1; i<=n; i++)
    {
        scanf(" %c %c %d", &u,&v,&w);
        a=u-'A'+1; b=v-'A'+1;
        if(w<dp[a][b])
        dp[a][b]=dp[b][a]=w;
    }
    for(i=1; i<=58; i++)
     dis[i]=dp[26][i];
    memset(book,0,sizeof(book));
    book[26]=1;
    for(i=1; i<=58; i++)
    {
        min=inf;
        for(j=1; j<=58; j++)
        {
            if(book[j]==0&&dis[j]<=min)
            {
                min=dis[j];u=j;
            }
        }
        book[u]=1;
        for(v=1; v<=58; v++)
        {
            if(dp[u][v]<inf)
            {
                if(dis[v]>dis[u]+dp[u][v])
                dis[v]=dis[u]+dp[u][v];
            }
        }
    }
    min=inf;
    for(i=1; i<26; i++)
    {
        if(dis[i]<min)
            min=dis[i];
    }
    for(i=1; i<26; i++)
     {
        if(min==dis[i])
        {
           printf("%c %d\n",'A'+i-1, min); break;
        }
     }
    }
    return 0;
}
### USACO Competition Problem Solutions for Cow Games In the context of USACO competitions, problems involving cows often require a blend of algorithmic thinking and mathematical insight. For instance, one notable problem involves Farmer John providing hay to his cows on different days with varying quantities[^3]. This type of scenario can be modeled using dynamic programming or greedy algorithms depending on what is asked. For specific game-related challenges featuring cows within USACO contests, consider an example where cows play games that involve strategic decision-making under given constraints. These scenarios frequently test contestants' ability to apply concepts like graph theory, number manipulation, and optimization techniques effectively. A relevant exercise from similar competitive coding platforms includes dealing with round numbers which have properties making them interesting subjects for computational puzzles[^2]: ```python def count_round_numbers(n): binary_representation = bin(n)[2:] zero_count = binary_representation.count('0') return zero_count >= len(binary_representation) / 2 ``` This function checks whether a number has at least as many zeros as ones in its binary representation—a concept sometimes explored through playful contexts such as virtual cow activities designed around numerical patterns. To tackle these kinds of tasks successfully: - Understand all rules governing how elements interact. - Identify efficient ways to represent data structures involved. - Develop strategies based on observed trends or established theories applicable to the situation described.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮皮皮皮皮皮卡乒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值