Blue Bridge Cup---Network Analysis (bfs)

Network analysis

Problem description
Xiao Ming is doing a network experiment. He set up n computers, called nodes, for sending, receiving and storing data. Initially, all nodes are independent and there is no connection. Xiao Ming can connect two nodes through a network cable, and the two nodes can communicate with each other after the connection. If two nodes are connected by a network cable, they are called adjacent. Xiao Ming sometimes tests the network at that time. He will send a message at a node, and the message will be sent to each neighboring node, and then these nodes will be forwarded to their neighbors until all directly or indirectly neighbors The nodes have all received the information. All sending and receiving nodes will store the information. A piece of information is stored only once. Given the process of Xiaoming's connection and testing, please calculate the size of the information stored in each node.

Input format
The first line of input contains two integers n, m, which represent the number of nodes and the number of operations respectively. The nodes are numbered from 1 to n.
In the next m lines, three integers in each line represent an operation.
If the operation is 1 ab, it means that node a and node b are connected by a network cable. When a = b, it means that a self-loop is connected, which has no substantial impact on the network.
If the operation is 2 pt, it means that a message of size t is sent on node p.

Output format
Output a line, containing n integers, separated by a space between adjacent integers, indicating the progress in turn

The size of the information stored on node 1 to node n after the above operations.

Sample input

4 8
1 1 2
2 1 10
2 3 5
1 4 1
2 2 2
1 1 2
1 2 4
2 2 1

Sample output

13 13 5 3

Idea: bfs and bfs are common knowledge points! ! !
Test data
For 30% of the evaluation cases, 1 ≤ n ≤ 20 and 1 ≤ m ≤ 100.
For 50% of the evaluation cases, 1 ≤ n ≤ 100 and 1 ≤ m ≤ 1000.
For 70% of the evaluation cases, 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 10000.
For all evaluation cases, 1 ≤ n ≤ 10000, 1 ≤ m ≤ 100000, and 1 ≤ t ≤ 100.

code:

#include<stdio.h>
#include<string.h>
#define  INF    10000

int n,m;//设全局变量
int list[INF]={
    
    0};//list存放所求值
int M[INF][INF];//使用邻接矩阵放

void bfs(int node,int t){
    
    //广度优先遍历,将广度优先遍历结果存放在数组(队列中),最后再依次加t
	int head=1,tail=1;
	int queue[INF]={
    
    0};//队列,存放广度优先遍历序列
	int book[INF]={
    
    0};//book用于标记是否入队,1为入队,0为没有入队
	queue[tail]=node;
	book[node]=1;
	tail++;
	while(head<tail&&tail<=n){
    
    
		int now=queue[head];
		for(int i=1;i<=n;i++){
    
    
			if(M[now][i]==1&&book[i]==0&&now!=i){
    
    
				book[i]=1;
				queue[tail]=i;
				tail++;
			}
			if(tail>n)break;
		}
		head++;
	}
	for(int i=1;i<=head;i++){
    
    //依次加t
		list[queue[i]]+=t;
	}
}
int main(){
    
    
	scanf("%d %d",&n,&m);
	
	memset(M,0,sizeof M);
	
	for(int i=0;i<m;i++){
    
    
		int op,n1,n2;
		scanf("%d %d %d",&op,&n1,&n2);//op是操作数
		if(op==1){
    
    //op为1时,连线即可
			M[n1][n2]=1;
			M[n2][n1]=1;
		}
		if(op==2){
    
    //op为2时,bfs
			bfs(n1,n2);
		}
	}
	
	for(int i=1;i<=n;i++){
    
    //最后输出list就行
		printf("%d",list[i]);
		if(i<n)printf(" ");
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115013890