一本通 MST

黑暗城堡

类似最短路计数的问题,模拟prim的计数过程就好了,可以写成nlog

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e3+10;
const int inf = 0x3f3f3f3f;
const int mod = 2147483647;
const int M = 1e6+10;
int n,m;
int id[N];
int e[M],ne[M],w[M],h[N],idx;
void add(int a,int b,int c){
	e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
}
int cost[1010][1010];
int dist[1010];
bool vis[1010];


void spfa()
{
	queue<int>q;
	memset(dist,0x3f,sizeof dist);
	vis[1] = true;
	dist[1] = 0;
	q.push(1);
	
	while(q.size()){
		auto ver = q.front();
		//cout<<ver<<"\n";
		q.pop();
		vis[ver] = false; 
		for(int i=h[ver];~i;i=ne[i]){
			int j = e[i];
			
			if(dist[j]>dist[ver]+w[i]){
				dist[j] = dist[ver] + w[i];
				if(!vis[j]){
					q.push(j);
					vis[j] = true;
				}
			}
		}
	}
	
	
}
 
bool cmp(int a,int b){
	return dist[a]<dist[b];
}

void solve()
{
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=n;j++)
	  if(i==j)cost[i][j] = 0;else cost[i][j] = cost[j][i] = inf;
	
	memset(h,-1,sizeof h);
		  
	for(int i=1;i<=m;i++){
		int a,b,c;scanf("%lld%lld%lld",&a,&b,&c);
		if(c<cost[a][b])cost[a][b] = cost[b][a] = c;
		add(a,b,c);
		add(b,a,c);
	}	
	
	spfa();
	

	
	for(int i=1;i<=n;i++)id[i] = i;
	
	sort(id+1,id+1+n,cmp);
	
	int res = 1;
	
	for(int i=2;i<=n;i++){
		int cnt = 0;
		for(int j=1;j<i;++j){
			if(dist[id[i]]==dist[id[j]]+cost[id[j]][id[i]])cnt++;
		}
		cnt%=mod;
		if(cnt)res = (res*cnt)%mod;
	}
	
	cout<<res;
	
	

}

signed main()
{
	//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e3+10;
const int inf = 0x3f3f3f3f;
const int mod = 2147483647;
const int M = 1e6+10;
int n,m;
int e[M],ne[M],w[M],h[N],idx;
int dp[N];


void add(int a,int b,int c){
	e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
}
int dist[1010];
bool vis[1010];



void dijkstra(int num)
{
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>heap;
	if(!num)memset(dist,0x3f,sizeof dist);
	memset(vis,0,sizeof vis);
	dist[1] = 0;
	
	dp[1] = 1;//计数数组
	heap.push({0,1});
	
	while(heap.size()){
		auto t = heap.top();
		heap.pop();
		

		
		int distance = t.first,ver = t.second;
		if(vis[ver])continue;
		vis[ver] = true;
		
		for(int i=h[ver];~i;i=ne[i]){
			int j = e[i];
			if(dist[j]>=w[i]+distance){
				if(num)dp[j] ++; 
				dist[j] = dist[ver]+w[i];
				heap.push({dist[j],j});
			}
		}
		
	}
	

}



bool cmp(int a,int b){
	return dist[a]<dist[b];
}

void solve()
{
	scanf("%lld%lld",&n,&m);


	memset(h,-1,sizeof h);
		  
	for(int i=1;i<=m;i++){
		int a,b,c;scanf("%lld%lld%lld",&a,&b,&c);
		add(a,b,c);
		add(b,a,c);
	}	
	
	dijkstra(0);
	dijkstra(1);
	
	//for(int i=1;i<=n;i++)cout<<dist[i]<<" ";
	//cout<<"\n";
	
	int res = 1;
	for(int i=1;i<=n;i++)res = res*dp[i]%mod;
	cout<<res;
	

	
	

	

}

signed main()
{
	//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

新的开始

虚拟原点 + prim跑最小生成树

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n,q;


int g[1010][1010];
bool vis[N];
int dist[N];
int ans = 0;
void prim()
{
	
	memset(dist,0x3f,sizeof dist);
	dist[0] = 0;
	
	for(int i=0;i<=n;i++){
		int t = inf;
		int k;
		for(int j=0;j<=n;j++){
			if(dist[j]<t&&!vis[j]){
				t = dist[j],k = j;
			}
		}
		
		//cout<<k<<" "<<t<<"gaga\n";;
		
		ans+=t;
		vis[k] = true;
		
		for(int j=1;j<=n;j++)dist[j] = min(dist[j],g[k][j]);
		
	}
	
	
}

void solve()
{
	cin>>n;
	
	for(int i=1;i<=n;i++){
		int x;cin>>x;
		g[0][i] = x;
	}	
	
	
	for(int i=1;i<=n;i++)
	 for(int j=1;j<=n;j++)
	  cin>>g[i][j];
	  
	prim();
	
	cout<<ans;

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

北极通讯网络

书上给的是一个证明的结论 数据范围很小可以直接暴力乱搞

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n,k;
int p[N];
int find(int x){
	
	if(x!=p[x])p[x] = find(p[x]);
	return p[x];
}


struct Node{
	int x,y;
}node[N];


double get_dist(int a,int b){
	int dx = node[a].x - node[b].x;
	int dy = node[a].y - node[b].y;
	return sqrt(dx*dx+dy*dy);	
}

bool check(double mid){
	
	
	for(int i=1;i<=n;i++)p[i] = i;
	
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++){
	  	if(i==j)continue;
	  	double x = get_dist(i,j);
	  	if(x<=mid){
	  		int a = find(i),b = find(j);
	  		p[a] = b;
	  	}
	  }
	  
	set<int>s;
	for(int i=1;i<=n;i++)s.insert(find(i));
	return s.size()<=k;
	  
	
}

void solve()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>node[i].x>>node[i].y;
	
	
	
	double l = 0,r = 1e4;
	
	while(r-l>1e-4){
		double mid = (l+r)/2;
		if(check(mid))r = mid;
		else l = mid;
		//cnt++;
		//cout<<l<<" "<<r<<"\n";
		//if(cnt>=50)break;
	}
	
	printf("%.2lf",r);
	
	

}

signed main()
{
	//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

构造完全图

考虑Kruskal的过程,它每次对两个集合进行操作,每次都是只保留最短的那条边,因此每次操作两个集合的时候我们考虑集合里面的其他边对答案的贡献就好 然后乘法原理一下就可以得到答案了

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n,q;
struct Node{
	int a,b,w;
	bool operator<(const Node&W)const{
		return w<W.w;
	}
}node[N];

int p[N],sz[N];


int find(int x){
	if(x!=p[x])p[x] = find(p[x]);
	return p[x];
}

void solve()
{
	
	cin>>n;
	int res = 0;
	for(int i=1;i<n;i++){
		int a,b,c;cin>>a>>b>>c;
		node[i] = {a,b,c};
		res+=c;
	}
	
	
	
	sort(node+1,node+n);
	for(int i=1;i<=n;i++)p[i] = i,sz[i] = 1;
	
	for(int i=1;i<n;i++){
		
		
		int a = node[i].a,b = node[i].b,c = node[i].w;
		a = find(a),b = find(b);
		
		res = res + (sz[a]*sz[b]-1)*(c+1);
		
		p[a] = b;
		sz[b]+=sz[a];
	}
	
	cout<<res;
	
	

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

秘密的奶牛运输

暴力求进行严格次小生成树

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 510,M = 2e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n,m;
int e[M],ne[M],w[M],h[N],idx;
int dist1[N][N];
int dist2[N][N];
void add(int a,int b,int c){
	e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
}
struct Node{
	int a,b,w,f;
	bool operator<(const Node&W)const{
		return w<W.w;
	}
}node[M];
int p[N];
int sum;
int find(int x){
	if(x!=p[x]) p[x] = find(p[x]);
	return p[x];
}


void dfs(int u, int fa, int maxd1, int maxd2, int d1[], int d2[])
{
    d1[u] = maxd1, d2[u] = maxd2;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j != fa)
        {
            int td1 = maxd1, td2 = maxd2;
            if (w[i] > td1) td2 = td1, td1 = w[i];
            else if (w[i] < td1 && w[i] > td2) td2 = w[i];
            dfs(j, u, td1, td2, d1, d2);
        }
    }
}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>node[i].a>>node[i].b>>node[i].w;
	}
	
	
	sort(node+1,node+1+m);
	
	for(int i=1;i<=n;i++)p[i] = i;
	memset(h,-1,sizeof h);
	for(int i=1;i<=m;i++){
		int a = node[i].a,b = node[i].b,c = node[i].w;
		int pa = find(a),pb = find(b);
		if(pa==pb)continue;
		node[i].f = 1;
		add(a,b,c),add(b,a,c);
		sum+=c;
		p[pa] = pb; 
	}
	
	
	
	for(int i=1;i<=n;i++)dfs(i,-1,-inf,-inf,dist1[i],dist2[i]);
	
	
	
	int res=1e15;
	for(int i=1;i<=m;i++){
		if(node[i].f)continue;
		int t = inf;
		int a = node[i].a,b = node[i].b,c = node[i].w;
		if(c>dist1[a][b])t = sum+c-dist1[a][b]; 
		else if(c>dist2[a][b])t = sum+c-dist2[a][b];
		
		res = min(res,t);
	}
	
	cout<<res;

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

Tree

一开始想到的是直接贪心的加能加上的最小的几条白边,其实不是最优的

这里有个经典的套路我之前没怎么接触过就是 带权二分答案,当你求某个东西出现的次数恰好是k的时候让你最优化问题,你可以直接做,通过给这个东西一个权值让它的出现次数呈现单调性也就可以二分答案来求了

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n,m,k;


struct Node{
	int a,b,w,c;
	bool operator<(const Node&W)const{
		if(W.w!=w)return w<W.w;
		return c<W.c;
	}
}node[N];
int p[N];

int s;

int find(int x){
	if(x!=p[x])p[x] = find(p[x]);
	return p[x];
}

int check(int mid){
	for(int i=0;i<=n;i++)p[i] = i;
	s = 0;
	for(int i=1;i<=m;i++)if(node[i].c==0)node[i].w-=mid;
	sort(node+1,node+1+m);
	int cnt = 0;
	for(int i=1;i<=m;i++){
		int a = find(node[i].a),b = find(node[i].b);
		if(a==b)continue;
		p[a] = b;
		s+=node[i].w;
		if(!node[i].c)cnt++;
		
	}
	
	for(int i=1;i<=m;i++)if(node[i].c==0)node[i].w+=mid;
	//cout<<cnt<<"\n";
	return cnt;
	
	
	
}
void solve()
{
	//你想控制答案中某个东西的恰好的出现次数
	//可以使用带权二分答案
	cin>>n>>m>>k;
	
	for(int i=1;i<=m;i++){
		int a,b,w,c;cin>>a>>b>>w>>c;
		node[i] = {a,b,w,c};
	}
	
	int l = -1e5,r = 1e5;
	while(l+1!=r){
		int mid = (l+r)/2;
		if(check(mid)>=k)r = mid;
		else l = mid;
		//cout<<l<<" "<<r<<"\n";
	}
	
	check(r);
	cout<<s+k*r<<"\n";
	
	
	

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

最小生成树计数

调了一下午 不知道自己死在哪里了,发现这道题并查集不能用按秩合并,因为要dfs恢复现场

恶心坏了.......太菜了 吃一堑长一智吧~~

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 31011;

using namespace std;
int n,m;

struct Edge{
	int x,y,w;
	bool operator<(const Edge&W)const{
		return w<W.w;
	}
}edge[N];
int father[N];


struct Node{
	int l,r;
	int cnt;
}a[N];
int tot;
int sum;

int Find(int x){
	if(x!=father[x])return Find(father[x]);
	return father[x];
}


int Kruskal()
{
	for(int i=1;i<=n;i++)father[i] = i;
	
	sort(edge+1,edge+1+m);
	int cnt = 0;
	
	for(int i=1;i<=m;i++){
		if(edge[i].w!=edge[i-1].w){
			tot++;
			a[tot].l = i;
			a[tot-1].r = i-1;
		}
		
		int x = edge[i].x,y = edge[i].y;
		x = Find(x),y = Find(y);
		if(x==y)continue;
		
		father[x] = y;
		
		cnt++;
		a[tot].cnt++;
		
	}
	
	
	a[tot].r = m;
	
	
	return cnt;
}

void dfs(int x,int now,int s){
	
	
	if(now>a[x].r){
		//cout<<s<<"??\n";
		if(s==a[x].cnt)sum++;
		return;
	}
	
	int fx = Find(edge[now].x),fy = Find(edge[now].y);
	//cout<<fx<<" "<<fy<<"\n";
	if(fx!=fy){
		father[fx] = fy;
		dfs(x,now+1,s+1);
		father[fx] = fx;
		father[fy] = fy;
	}
	
	
	dfs(x,now+1,s);
}


void solve()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>edge[i].x>>edge[i].y>>edge[i].w;
	
	
	int num = Kruskal();
	
	if(num!=n-1){cout<<0;return;}
	

	
	int res = 1;
	for(int i=1;i<=n;i++)father[i] = i;
	
	for(int i=1;i<=tot;++i){
		sum = 0;
		
		dfs(i,a[i].l,0);
		//cout<<"??"<<"\n";
		//cout<<sum<<"\n";
		res = res*sum%mod;
		
		
		for(int j = a[i].l;j<=a[i].r;++j){
			int fx = Find(edge[j].x);
			int fy = Find(edge[j].y);
			if(fx!=fy)father[fx] = fy;
		}
	}
	

	
	
	cout<<res;

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值