jzoj 3205.【HNOI模拟题】Dual-SIM Phone 暴力+二分答案

Description
彼得,一个学生,想要从短信业务中获利。当然,他也想花最少的钱发送信息,并且尽快地发送信息。因此,他想买一个双卡手机,对于两个运营商的卡可以同时工作。现在,彼得可以发送短信给某个电话号码,通过两个运营商中花钱更少的一个。

不幸的是,并非所有手机运营商可以通过他们发送短信给其他运营商的电话号码。帮助彼得选择一对运营商,使得他能够发送短信给所有运营商的电话号码,而且发送短信的最大费用最小。

Input
第一行包含用空格隔开的两个整数n和k。n是手机运营商的数目。接下来k行,每行含有整数x; y; c,表示彼得可以花费c元通过运营商x发送一条短信给运营商y的电话号码。

Output
输出发送一条短信最大的花费。如果不可能,输出"No solution"。

Sample Input
4 13
1 1 1
1 2 3
1 3 3
1 4 5
2 1 2
2 2 1
2 3 2
3 1 4
3 3 4
3 4 1
4 1 2
4 2 3
4 4 3

Sample Output
2

Data Constraint
对于20%的数据满足 n &lt; = 100 , m &lt; = 400 n &lt;= 100,m &lt;= 400 n<=100m<=400
对于50%的数据满足 n &lt; = 1000 , m &lt; = 4000 n &lt;= 1000,m &lt;= 4000 n<=1000m<=4000
对于100%的数据满足 2 &lt; = n &lt; = 1 0 4 , 0 &lt; = k &lt; = 1 0 5 , 1 &lt; = x , y &lt; = n , 1 &lt; = c &lt; = 1 0 9 2 &lt;= n &lt;= 10^4,0 &lt;= k &lt;= 10^5,1&lt;= x, y &lt;= n,1 &lt;= c &lt;= 10^9 2<=n<=1040<=k<=1051<=x,y<=n1<=c<=109

分析:
二分一个答案,每个点保留边权小于等于 m i d mid mid的边。
然后开一个bitset记录相连的所有点,暴力两个点判断并集是否为全集。
显然所有点bitset中只有 m m m个位置有 1 1 1,显然只有两个bitset中 1 1 1的位置个数和大于 n n n才有可能有解。
我们对所有点按 1 1 1的个数排序,只考虑用小的集合匹配大的集合。

显然,大的集合的1的个数至少为 n / 2 n/2 n/2,那么这种集合最多有 2 ∗ m / n 2*m/n 2m/n个。而小的集合不超过 n n n个,所以匹配数是 O ( m ) O(m) O(m)的。大集合之间的匹配基本可以忽略。但大集合变多时,意味着 n n n变小,其实匹配数也不会有太大变化。

总复杂度为 O ( m l o g n ∗ n / 32 ) O(mlogn*n/32) O(mlognn/32),应该是跑不满的。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <bitset>
#include <map>
#include <algorithm>

const int maxn=1e4+7;
const int maxe=2e5+7;

using namespace std;

int n,m,ans,cnt,x,y,w;
int num[maxn],c[maxe],sz[maxn],p[maxn];

struct rec{
	int x,y;
};

bool operator <(rec a,rec b)
{
	if (a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}

struct edge{
	int x,y,w;
}g[maxe];

bitset <maxn> a[maxn],b;
map <rec,int> h;

bool cmp1(edge a,edge b)
{
	if (a.x==b.x) return a.w<b.w;
	return a.x<b.x;
}

bool cmp2(int x,int y)
{
	return sz[x]<sz[y];
}

int main()
{
	scanf("%d%d",&n,&m);	
	for (int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&w);
		int k=h[(rec){x,y}];
		if (!k)
		{
			g[++cnt].x=x,g[cnt].y=y,g[cnt].w=w;
			c[cnt]=g[cnt].w;
			h[(rec){x,y}]=cnt;
		}
		else
		{
			if (w<g[k].w)
			{
				g[k].w=w;
			    c[k]=w;
			}
		}
	}			
	m=cnt;
	sort(c+1,c+m+1);
	int size=unique(c+1,c+m+1)-c-1;
	for (int i=1;i<=m;i++) g[i].w=lower_bound(c+1,c+size+1,g[i].w)-c;
	sort(g+1,g+m+1,cmp1);	
	for (int i=1;i<=m;i++)
	{
		if (!num[g[i].x]) num[g[i].x]=i;
	}
	for (int i=1;i<=n;i++) p[i]=i;
	int l=1,r=size;
	while (l<=r)
	{
		int mid=(l+r)/2;		
		for (int i=1;i<=n;i++)
		{
			if (num[i])
			{
				while ((g[num[i]].x==i) && (g[num[i]].w<=mid))
				{
					a[i][g[num[i]].y]=1,sz[i]++;
					num[i]++;
				} 
				while ((g[num[i]-1].x==i) && (g[num[i]-1].w>mid))
				{
					a[i][g[num[i]-1].y]=0,sz[i]--;
					num[i]--;
				} 
			}
		}		
		sort(p+1,p+n+1,cmp2);
		int flag=0;
		for (int i=1;i<=n;i++)
		{
			for (int j=n;j>i;j--)
			{
				int x=p[i],y=p[j];
				if (sz[x]+sz[y]<n) break;
				b=a[x]|a[y];
				if (b.count()==n)
				{
					flag=1;
					break;
				}
			}
		}
		if (flag) ans=mid,r=mid-1;
		     else l=mid+1;
	}
	if (ans) printf("%d",c[ans]);
	    else printf("No solution");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值