Ada and Cycle

在Bugindia的多个城市间,利用邻接矩阵表示城市间的单向道路,通过基础BFS算法寻找从每个城市出发并返回的最短路径长度。如果不存在这样的路径,则输出特定提示。

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

ADACYCLE - Ada and Cycle

no tags 

Ada the Ladybug is on a trip in Bugindia. There are many cities and some uni-directional roads connecting them. Ada is wondering about the shortest path, which begins in a city and ends in the same city. Since Ada likes short trips, she asked you to find the length of such path for each city in Bugindia.

Input

The first line will contain 0 < N ≤ 2000, the number of cities.

Then N lines follow, each containing N integers 0 ≤ Hij ≤ 1. One means, that there is a road between and (zero means there isn't a road).

Output

Print N lines, the length of shortest path which begins in city i and ends in city i. If the path doesn't exist, print "NO WAY" instead.

Example Input

5
0 1 1 1 1
1 0 0 0 1
0 0 1 1 0
0 0 1 0 0
0 0 0 1 0

Example Output

2
2
1
2
NO WAY

Example Input

5
0 1 0 0 1
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 0

Example Output

2
5
5
5
2

2.题意概述:

给你邻接矩阵,其中ij元素的0表示i到j没有道路,1表示有长度为1的道路,某人喜欢短途旅行,要你判断他在i城市时候的最短旅行路程是什么,不存在则输出NO WAY

3.解题思路

时间很宽松,因为每条边权值相同都为1,用最基础的bfs即可。


4.AC代码:


#include<bits/stdc++.h>
using namespace std;
const int N=2222;
const int inf=0x3f3f3f3f;
vector<int>vv[N];
bool vis[N];
int ans;
int n;
typedef pair<int,int> p;


int bfs(int i)
{
    queue<p>q;
    q.push(make_pair(0,i));

    while(!q.empty())
    {
        int top=q.front().second;
        int dis=q.front().first;
        q.pop();
        for(int j=0;j<vv[top].size();j++)
        {
            int tem=vv[top][j];
            if(!vis[tem])
            {
                vis[tem]=1;
                if(tem==i)
                    return  dis+1;
                    q.push(make_pair(dis+1,tem));
            }

        }
    }
    return inf;
}
int main()
{
    cin>>n;
    int tem;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&tem);
            if(tem)
            {
                vv[i].push_back(j);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        ans=inf;
        if(vv[i].size())
        {
            memset(vis,false,sizeof(vis));
            ans=bfs(i);
        }
        if(ans==inf)
        {
            puts("NO WAY");
        }
        else
        {
            printf("%d\n",ans);
        }
    }
}


``` module sort_4 ( input clk, input rst_n, input [63:0] data_in, // 16x4bit输入 output reg [63:0] data_out // 16x4bit输出 ); // ------------------------------ // 寄存器与变量声明 // ------------------------------ reg [3:0] data_reg [0:15]; // 数据寄存器数组 reg [1:0] cycle_cnt; // 周期计数器(0~3) integer i; // 循环变量 // ------------------------------ // 主排序逻辑 // ------------------------------ always @(posedge clk or negedge rst_n) begin if (!rst_n) begin // 复位时加载输入数据 cycle_cnt <= 2'b0; for (i = 0; i < 16; i = i + 1) begin data_reg[i] <= data_in[i*4 +:4]; // 位拼接:每4bit为一个数 end end else begin if (cycle_cnt < 2'd3) begin cycle_cnt <= cycle_cnt + 1; // 根据周期执行不同的比较交换逻辑 case (cycle_cnt) 0: compare_stage0(); // 周期1:长距离比较对 1: compare_stage1(); // 周期2:中距离比较对 2: compare_stage2(); // 周期3:短距离比较对 3: compare_stage3(); // 周期4:最终调整 endcase end else begin // 排序完成,输出结果 for (i = 0; i < 16; i = i + 1) begin data_out[i*4 +:4] <= data_reg[i]; end end end end // ------------------------------ // 各周期比较交换逻辑(关键设计) // ------------------------------ // 周期1:长距离比较对(步长8、4) task compare_stage0; begin // 步长8比较对:0-8,1-9,...,7-15 for (i = 0; i < 8; i = i + 1) begin compare_and_swap(i, i+8); end // 步长4比较对:0-4,1-5,...,11-15 for (i = 0; i < 12; i = i + 1) begin compare_and_swap(i, i+4); end end endtask // 周期2:中距离比较对(步长2、1) task compare_stage1; begin // 步长2比较对:0-2,1-3,...,13-15 for (i = 0; i < 14; i = i + 1) begin compare_and_swap(i, i+2); end // 步长1比较对:0-1,1-2,...,14-15 for (i = 0; i < 15; i = i + 1) begin compare_and_swap(i, i+1); end end endtask // 周期3:短距离比较对(步长1、2) task compare_stage2; begin // 步长1反向比较对:15-14,...,1-0 for (i = 14; i >= 0; i = i - 1) begin compare_and_swap(i, i+1); end // 步长2反向比较对:14-12,...,2-0 for (i = 12; i >= 0; i = i - 2) begin compare_and_swap(i, i+2); end end endtask // 周期4:最终调整 task compare_stage3; begin // 全步长1比较对 for (i = 0; i < 15; i = i + 1) begin compare_and_swap(i, i+1); end end endtask // ------------------------------ // 单次比较交换操作(阻塞赋值) // ------------------------------ task compare_and_swap; input integer idx1, idx2; reg [3:0] temp; begin if (data_reg[idx1] < data_reg[idx2]) begin temp = data_reg[idx1]; data_reg[idx1] = data_reg[idx2]; data_reg[idx2] = temp; end end endtask endmodule```datain=64‘hf334ada226185962 dataout= 64’h22396568a9aacdef 为什么这组数据排完序后有两个数是错的,但是随机生成其他的数据有的是正确的
03-28
内容概要:本文档定义了一个名为 `xxx_SCustSuplier_info` 的视图,用于整合和展示客户(Customer)和供应商(Supplier)的相关信息。视图通过连接多个表来获取组织单位、客户账户、站点使用、位置、财务代码组合等数据。对于客户部分,视图选择了与账单相关的记录,并提取了账单客户ID、账单站点ID、客户名称、账户名称、站点代码、状态、付款条款等信息;对于供应商部分,视图选择了有效的供应商及其站点信息,包括供应商ID、供应商名称、供应商编号、状态、付款条款、财务代码组合等。视图还通过外连接确保即使某些字段为空也能显示相关信息。 适合人群:熟悉Oracle ERP系统,尤其是应付账款(AP)和应收账款(AR)模块的数据库管理员或开发人员;需要查询和管理客户及供应商信息的业务分析师。 使用场景及目标:① 数据库管理员可以通过此视图快速查询客户和供应商的基本信息,包括账单信息、财务代码组合等;② 开发人员可以利用此视图进行报表开发或数据迁移;③ 业务分析师可以使用此视图进行数据分析,如信用评估、付款周期分析等。 阅读建议:由于该视图涉及多个表的复杂连接,建议读者先熟悉各个表的结构和关系,特别是 `hz_parties`、`hz_cust_accounts`、`ap_suppliers` 等核心表。此外,注意视图中使用的外连接(如 `gl_code_combinations_kfv` 表的连接),这可能会影响查询结果的完整性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值