算法设计与分析基础 第九章谣言传播问题

文章探讨了在不同条件下如何设计贪心算法确保所有人都能获取所有谣言,同时最小化信息发送数量。在时间成本为1的情况下,最优解是线性传递。对于非均匀时间成本,通过DFS实现了一种解决方案。此外,还对比了POJ1125问题,该问题关注从单点传播的最短时间,并提供了Dijkstra和Floyd算法的实现。
摘要由CSDN通过智能技术生成

谣言传播 

有n个人,每个人都拥有不同的谣言。通过发电子信息,他们相互想共享所有的谣言。假定发送者会在信息中包含他已知的所有谣言,而且一条信息只有一个收信人。设计一个贪心算法,保证再给个人都能获得所有谣言的条件下,是发送的信息数最小。

首先考虑每个人之间发送短信都是消耗的时间都是1,则最优解一定为任意一个人1号发送短信,之后2号收到一号的谣言之后,发送1、2号的谣言给3,以此类推,最后收到短信的n就拥有了所有人的谎言,然后n给所有人发送自己的所有谣言。总消耗恒为2n-2。

其次考虑每任意两个人之间发送短信消耗的时间不一致的情况,下面是dfs版的谣言传播问题代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=510;
int n,m,dist[N],g[N][N],res=1e9+10;
int a[N];
void dfs(int x,bool st[],int ans,int b[]){
	if(x==n){
		if(ans+dist[b[x-1]]<res){
			for(int i=0;i<n;i++){
				a[i] = b[i];
			}
			res = ans+dist[b[x-1]];
		}
		for(int i=0;i<n;i++){
			printf("%d ",b[i]);
		}
		puts("");
		return ;
	}
	else{
		for(int i=1;i<=n;i++){
			if(!st[i]&&(g[b[x-1]][i]<0x3f||x==0)){
				st[i] = true;
				b[x] = i;
				if(x!=0)dfs(x+1,st,ans+g[b[x-1]][i],b);
				else dfs(x+1,st,0,b);
				st[i] =false;
			}
		}
	}
}

int main(void){
	cin>>n>>m;
	memset(g,0x3f,sizeof g);
	for(int i=1;i<=m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		g[a][b] = min(g[a][b],c);
	}
	for(int i=1;i<=n;i++){g[0][i]=0;g[i][0]=0;}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i!=j)dist[i] += g[i][j];
		}
	}
	cout<<endl;
	bool st[N]={false};
	int b[N];
	
	dfs(0,st,0,b);
	for(int i=0;i<n;i++){
		cout<<a[i]<<" ";
	}
	cout<<endl<<res;
	return  0;
}

示例:

3 6
1 2 4
1 3 5
2 1 2
2 3 6
3 1 2
3 2 2

输出

2 1 3

11

其次我们拓展开来看POJ 1125的谣言传播问题,与本题类似但是有本质的区别,POJ 1125是从求一个人开始传播所耗费的最短时间,并且一个人可以同时给多个人同时发短信。所以只取最长的作为耗费的时间

n次迪杰斯特拉版本的代码,可以参考下面的博客:

谣言传播_算法 谣言传播问题_MR. Dong的博客-CSDN博客

不使用dijkstra使用floyd算法版本如下:

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxnum = 100;
const int maxint = 1e9+10;
int dist[maxnum][maxnum]; 
int n, line; 
int p, q, len; 
void initation(){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
		if(i==j)dist[i][j] = 0;
		else dist[i][j] = maxint;
	}
}

void floyd(){
	for(int k = 1;k<=n;k++)
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
}

void searchPath(int *prev, int v, int u)
{
	int que[maxnum];
	int tot = 1;
	que[tot] = u;
	tot++;
	int tmp = prev[u];
	while (tmp != v)
	{
		que[tot] = tmp;
		tot++;
		tmp = prev[tmp];
	}
	que[tot] = v;
	for (int i = tot; i >= 1; --i)
		if (i != 1)
			cout << que[i] << " -> ";
	else
		cout << que[i] << endl;
}

void YuanDian(int n, int m[maxnum]) {
	for (int i = 1; i <= n; i++) {
		m[i] = dist[i][1];
		for (int j = 1; j <= n; j++) {
			if (m[i] < dist[i][j] && i != j) {
				m[i] = dist[i][j];
			}
		}
	}
	int min = m[1];
	int a = 0;
	for (int k = 1; k <= n; k++) {
		if (min > m[k]) {
			min = m[k];
			a = k;
		}
	}
	cout << "应从" << a << "开始" << "时间最短为" << min << endl;
}

int main()
{
	// 各数组都从下标1开始
	int m[maxnum];
	// 输入结点数
	cin >> n;
	// 输入路径数
	cin >> line;
	initation();
	for (int i = 0; i < line; ++i)
	{
		cin >> p >> q >> len;
		dist[p][q] = min(len,dist[p][q]);
	}
	floyd();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++)cout<<dist[i][j]<<" ";
		cout<<endl;
	}
	YuanDian(n, m);
	//system("pause");
	//searchPath(prev, 1, k);
}

还有其他floyd版本代码可以见:


POJ 1125 谣言传播速度_ych_ding的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值