【宽搜】ECNA 2015 E Squawk Virus (Codeforces GYM 100825)

本文介绍了Codeforces GYM 100825题目的解法,该问题涉及网络中病毒传播的模拟。题目要求在限定时间内,通过广度优先搜索(宽搜)策略,计算经过T个时间单位后,图中病毒的总数量。初始状态为一个节点S携带1个病毒,病毒每单位时间传递一次,并在传递后清零。解题思路是将图分为奇偶时间层,病毒从一层传播到另一层,最终统计病毒总数。

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

题目链接:

  http://codeforces.com/gym/100825

题目大意:

  N个点M条无向边,(N<=100,M<=N(N-1)/2),起始感染源S,时间T(T<10),每个感染源会在1个单位时间内给与自己相连的所有点传递与自身病毒数相等的病毒,然后自己的病毒数归零,被感染点在1个单位时间之后会收到病毒。

  求T时刻这张图上的总病毒数。(初始时刻为0,S带有1个病毒)

题目思路:

  【宽搜】

  这题因为T很小,N也很小所以可以暴力宽搜,把图按时间奇偶分成两层,每次病毒都从一层中的感染源传递到另一层中与感染源相邻的点。

  最后统计答案即可。




//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 104
#define M 10004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int s,t;
int last[N];
LL d[2][N];
int q[2][N];
bool u[2][N];
struct edge
{
	int next,to;
}a[M];
void add(int x,int y)
{
	a[++lll].to=y;
	a[lll].next=last[x];
	last[x]=lll;
}
void spfa()
{
	int i,now,to,k=1,tt,l[2],r[2];
	mem(d,0);mem(u,0);
	l[0]=l[1]=r[0]=r[1]=0;
	for(i=last[s];i;i=a[i].next)
	{
		q[0][++r[0]]=a[i].to;
		d[0][a[i].to]=1;
	}
	for(tt=0;tt<t-1;tt++)
	{
		k=tt&1;
		while(l[k]<r[k])
		{
			now=q[k][++l[k]];
			for(i=last[now];i;i=a[i].next)
			{
				to=a[i].to;
				d[1-k][to]+=d[k][now];
				if(!u[1-k][to])
				{
					u[1-k][to]=1;
					q[1-k][++r[1-k]]=to;
				}
			}
		}
		l[k]=r[k]=0;
		mem(d[k],0);mem(u[k],0);
	}
	for(i=1;i<=n;i++)
		aans+=d[1-k][i];
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z;
//	init();
//	for(scanf("%d",&cass);cass;cass--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
	while(~scanf("%d",&n))
	{
		aans=0;lll=0;mem(last,0);
		scanf("%d%d%d",&m,&s,&t);
		s++;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			x++,y++;
			add(x,y),add(y,x);
		}
		if(t==0){puts("0");continue;}
		spfa();
		printf("%I64d\n",aans);
	}
	return 0;
}
/*
//

//
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值