黑暗城堡

题目描述】
知道黑暗城堡有 N 个房间,M 条可以制造的双向通道,以及每条通道的长度。
城堡是树形的并且满足下面的条件:
设 Di 为如果所有的通道都被修建,第 i号房间与第 1 号房间的最短路径长度;
而 Si 为实际修建的树形城堡中第 i 号房间与第 1 号房间的路径长度;
要求对于所有整数 i(1≤i≤N) ,有 Si=Di 成立。
你想知道有多少种不同的城堡修建方案。当然,你只需要输出答案对 231−1231−1 取模之后的结果就行了。
【输入】
第一行为两个由空格隔开的整数 N,MN,M ;
第二行到第 M+1行为 3 个由空格隔开的整数 x,y,l :表示 x 号房间与 y 号房间之间的通道长度为 l 。
【输出】
一个整数:不同的城堡修建方案数对 231−1231−1 取模之后的结果。
【输入样例】
4 6
1 2 1
1 3 2
1 4 3
2 3 1
2 4 2
3 4 1
【输出样例】
6
最终形成的路径一定是最短的。先用迪杰斯特拉求出求短路径dis数组,再dis[i]=dis[j]+c[j][i],cnt++;记录每个i可以有多少条路径,最后在相乘。

#include<stdio.h>
#include<math.h>
#include<algorithm>
const long long  N=pow(2,31)-1;
using namespace std;
int inf=0x3f3f3f3f;
const int K=6e3;
int c[K][K],n,m;
int dis[K];
int book[K];
void memset()
{
    int i,j;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
            if(i==j)
                c[i][j]=0;
            else
                c[i][j]=inf;
    }

}
void djk()
{
    int i,k=m,j;
    for(i=1; i<=n; i++)
        dis[i]=c[1][i];
    dis[1]=0;
    book[1]=1;
    while(k--)
    {
        int u,minn=inf;
        for(i=1; i<=n; i++)
        {
            if(dis[i]<minn&&book[i]==0)
            {
                u=i;
                minn=dis[i];
            }
        }
        book[u]=1;
        for(j=1; j<=n; j++)
        {
            if(c[u][j]<inf)
            {
                if(dis[j]>dis[u]+c[u][j]&&i!=j)
                {
                    dis[j]=dis[u]+c[u][j];
                }
            }
        }
    }
}
int main()
{
    int i,j;
    scanf("%d %d",&n,&m);
    memset();
    for(i=0; i<m; i++)
    {
        int x,y,z;
        scanf("%d %d %d",&x,&y,&z);
        if(c[x][y]>z)
            c[x][y]=c[y][x]=z;
    }
    djk();
    long long ans=1,cnt=0;
    for(i=2; i<=n; i++)
    {
        cnt=0;
        for(j=1; j<=n; j++)
        {
            if(dis[j]+c[j][i]==dis[i])
                cnt++;
        }
        ans=(ans*cnt)%N;

    }
    printf("%lld\n",ans);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基本的Python海龟命令画城堡的示例代码: ```python import turtle # 设置画笔 t = turtle.Pen() t.speed(0) # 设置画笔速度为最快 # 画城堡墙壁 t.pensize(5) t.color('gray') t.begin_fill() for i in range(4): t.forward(200) t.right(90) t.end_fill() # 画城堡门 t.penup() t.goto(80, -50) t.pendown() t.color('brown') t.begin_fill() t.forward(40) t.left(90) t.forward(80) t.left(90) t.forward(40) t.left(90) t.forward(80) t.end_fill() # 画城堡旗帜 t.penup() t.goto(0, 110) t.pendown() t.color('red') t.begin_fill() t.forward(40) t.left(120) t.forward(80) t.left(120) t.forward(80) t.left(120) t.forward(40) t.end_fill() # 画城堡塔 t.penup() t.goto(-100, 0) t.pendown() t.color('gray') t.begin_fill() t.circle(50) t.end_fill() # 画城堡塔顶 t.penup() t.goto(-100, 100) t.pendown() t.begin_fill() t.color('brown') t.circle(20) t.end_fill() # 画城堡另一个塔 t.penup() t.goto(100, 0) t.pendown() t.color('gray') t.begin_fill() t.circle(50) t.end_fill() # 画城堡另一个塔顶 t.penup() t.goto(100, 100) t.pendown() t.begin_fill() t.color('brown') t.circle(20) t.end_fill() # 隐藏画笔 t.hideturtle() # 点击关闭窗口 turtle.exitonclick() ``` 代码解释: - 创建一个海龟对象t。 - 画城堡墙壁:设置画笔粗细和颜色,然后使用for循环画出四个边长为200的正方形。 - 画城堡门:使用penup()抬起画笔,goto()移动到门的位置,pendown()放下画笔,然后画出门的形状。 - 画城堡旗帜:使用penup()抬起画笔,goto()移动到旗帜的位置,pendown()放下画笔,然后画出旗帜的形状。 - 画城堡塔:使用penup()抬起画笔,goto()移动到塔的位置,pendown()放下画笔,然后画出塔的形状。 - 画城堡塔顶:使用penup()抬起画笔,goto()移动到塔顶的位置,pendown()放下画笔,然后画出塔顶的形状。 - 隐藏画笔,然后等待用户单击窗口关闭程序。 运行上述代码后,将会在窗口中画出一个城堡

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值