CSP 201912-4 区块链(C)

文章目录

题目

在这里插入图片描述
解题思路大家可以去我上一篇博客看。

代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Point{//节点 
	int len;
	int link[501];
};
struct upcom{//更新命令 
	int time;
	int len_d;
	int dist[501];
	int len;
	int link[501];
	upcom* next;
};
upcom* fq,*lq;//首尾指针 
int m,n,t,k;
int** G;//存储邻接表 
Point* point;//节点 
int co[3];//命令

void addcom(int dist,int time){//添加更新命令
	upcom *temp;
	temp=(upcom*)malloc(sizeof(upcom)); 
	int count=0;
	for(int i=1;i<=n;i++){
		if(G[dist][i]==1||G[i][dist]==1)
			temp->dist[count++]=i;
	}
	if(count>0){//添加命令
		temp->len_d=count;
		temp->time=time;
		temp->len=point[dist].len;
		for(int j=1;j<=temp->len;j++)
			temp->link[j]=point[dist].link[j];
		if(fq==NULL){
			fq=temp;
			lq=temp;
			lq->next=NULL;
		}
		else{
			lq->next=temp;
			lq=temp;
			lq->next=NULL;
		}
	}
}
void update(int T){//处理更新命令 
	upcom *cur;
	cur=fq;
	while(cur!=NULL&&cur->time<=T){
		for(int i=0;i<cur->len_d;i++){
			if(cur->len>point[cur->dist[i]].len||
			(cur->len==point[cur->dist[i]].len &&
			cur->link[cur->len]<point[cur->dist[i]].link[cur->len])){
				point[cur->dist[i]].len=cur->len;
				for(int j=1;j<=cur->len;j++)
					point[cur->dist[i]].link[j]=cur->link[j];
				addcom(cur->dist[i],cur->time+t);
			}
		}
		cur=cur->next;
		free(fq);
		fq=cur;
	}
}
int main(void){ 
	char command[10];
	scanf("%d %d",&n,&m);
	
	point=(Point*)malloc((n+1)*(sizeof(Point)));//初始化节点状态 
	for(int i=1;i<=n;i++){
		point[i].len=1;
		point[i].link[1]=0;
	}
	
	G=(int**)malloc(sizeof(int*)*(n+1));
	for(int i=0;i<=n;i++)
		G[i]=(int*)malloc(sizeof(int)*(n+1));
	memset(G,0,sizeof(G));//初始化邻接表
	 
	for(int i=0;i<m;i++){//读取边 
		int u,v;
		scanf("%d %d",&u,&v);
		G[u][v]=1;	
	}	
	fq=NULL;//初始化首尾指针 
	lq=NULL;

	scanf("%d %d",&t,&k);//读取t和k
	getchar();
	for(int i=0;i<k;i++){//读取命令 
		memset(co,0,sizeof(co));
		gets(command);
		int count=0;
		int j=0;
		while(command[j]!='\0'){//将字符转化为数字 
			if(command[j]==' ')
				count++;
			else
				co[count]=co[count]*10+command[j]-'0';
			j++;
		}
		update(co[1]);//先做有效的更新命令 
		if(count==2){//如果是创建新块的命令 
			point[co[0]].len++;
			point[co[0]].link[point[co[0]].len]=co[2];
			addcom(co[0],co[1]+t);
		}
		else{//如果是查询命令 
			printf("%d",point[co[0]].len);
			for(int k=1;k<=point[co[0]].len;k++)
				printf(" %d",point[co[0]].link[k]);
			printf("\n");
		} 
	}
	return 0;
}

样例1输入:

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
1 27
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
1 10 10
2 11 9
1 11
2 11
3 11
4 11
5 11
1 12
2 12
3 12
4 12
5 12

样例2输入:

15 13
1 2
2 3
3 4
4 5
1 6
6 7
7 8
8 9
1 10
10 11
11 12
12 13
14 15
6 28
1 1 1
1 2 2
1 6
2 7
13 7
9 7
5 7
3 14
8 14
5 14
11 14
9 25
5 25
13 25
9 29 3
5 29 4
13 29 5
1 53
2 59 6
2 59
1 1000
3 1000
8 1000
9 1000
10 1000
13 1000
14 1000
15 1000

结果

在这里插入图片描述

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值