codeforces1196F

题目描述

在这里插入图片描述
在这里插入图片描述

题目大意

给出一张图,求从1~n出发得到的最短路总数的K短路(保证u–>v,u<v)是多少

乱搞

显然可以想到二分答案+判断
但可能会因为二分结果太大导致耗时很多(其实也并不多),于是加上类似(?)迭代加深的做法,每次在一段里二分,如果没找到就在下一段里找
结果在我用正解A了之后才发现被hack了

code

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define min(a,b) (a<b?a:b)
using namespace std;

int ls[200001];
int a[400001][3];
int d[10000001];
bool bz[200001];
bool Bz[200001];
long long f[200001];
int n,m,K,i,j,k,len,tot,h,t;
long long l,r,mid,mn;

void New(int x,int y,int z)
{
	++len;
	a[len][0]=y;
	a[len][1]=ls[x];
	a[len][2]=z;
	ls[x]=len;
}

void bfs(int T,long long t2)
{
	int i;
	
	h=0;
	t=1;
	d[1]=T;
	bz[T]=1;
	Bz[T]=1;
	f[T]=0;
	
	while (h<t)
	{
		for (i=ls[d[++h]]; i; i=a[i][1])
		{
//			cout<<d[h]<<" "<<a[i][0]<<"   "<<f[d[h]]+a[i][2]<<" "<<t<<"   "<<f[a[i][0]]<<" "<<f[d[h]]+a[i][2]<<endl;
			
			if (f[d[h]]+a[i][2]<=t2 && f[a[i][0]]>f[d[h]]+a[i][2])
			{
//				cout<<d[h]<<" "<<a[i][0]<<endl;
				
				f[a[i][0]]=f[d[h]]+a[i][2];
				if (!Bz[a[i][0]])
				{
					Bz[a[i][0]]=1;
					++tot;
				}
				
				if (!bz[a[i][0]])
				{
					bz[a[i][0]]=1;
					d[++t]=a[i][0];
//					cout<<a[i][0]<<endl;
				}
				
				if (tot>=K)
				break;
			}
		}
		
		bz[d[h]]=0;
		
		if (tot>=K)
		break;
	}
	
//	cout<<tot<<" "<<K<<endl;
	
//	fo(i,1,n) cout<<f[i]<<" ";cout<<endl;
	
//	memset(bz,0,sizeof(bz));
//	memset(Bz,0,sizeof(bz));
//	memset(f,127,sizeof(f));
	fo(i,1,t)
	{
		bz[d[i]]=0;
		Bz[d[i]]=0;
		f[d[i]]=9223372036854775807ll;
	}
}

bool pd(long long t)
{
	int i;
	
	tot=0;
	fo(i,1,n)
	{
		bfs(i,t);
//		cout<<t<<" "<<tot<<endl;
		if (tot>=K)
		return 1;
	}
	
	return 0;
}

int main()
{
//	freopen("f.in","r",stdin);
	
	memset(f,127,sizeof(f));
	
	scanf("%d%d%d",&n,&m,&K);
	mn=1000000000;
	K*=2;
	
	fo(i,1,m)
	{
		scanf("%d%d%d",&j,&k,&l);
		mn=min(mn,l);
		
		New(j,k,l);
		New(k,j,l);
	}
	
	l=mn;
	while (!pd(l))
	{
//		r=l+1000000000000ll;
		r=l+10000000000ll;
		while (l<r)
		{
			mid=(l+r)/2;
			
			if (!pd(mid))
			l=mid+1;
			else
			r=mid;
		}
	}
	
	printf("%I64d\n",l);
}

正解

显然答案不会超过排序后第K小的边
所以只需要取前K条边,最多2K个点来跑Floyd就够了

code

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define min(a,b) (a<b?a:b)
using namespace std;

struct type{
	int x,y,s;
} A[200001];
long long a[801][3];
long long f[801][801];
int ls[200001];
bool bz[200001];
int id[200001];
long long ans[640001];
int N,n,m,K,i,j,k,l,len;

bool cmp(type a,type b)
{
	return a.s<b.s;
}

void New(int x,int y,int z)
{
	++len;
	a[len][0]=y;
	a[len][1]=ls[x];
	ls[x]=len;
	a[len][2]=z;
}

int main()
{
//	freopen("f.in","r",stdin);
	
	scanf("%d%d%d",&n,&m,&K);
	fo(i,1,m)
	scanf("%d%d%d",&A[i].x,&A[i].y,&A[i].s);
	
	sort(A+1,A+m+1,cmp);
	
	fo(i,1,K)
	{
		bz[A[i].x]=1;
		bz[A[i].y]=1;
	}
	
	j=0;
	fo(i,1,n)
	if (bz[i])
	id[i]=++j;
	N=j;
	
	memset(f,1,sizeof(f));
	fo(i,1,N)
	f[i][i]=0;
	
	fo(i,1,K)
	{
		A[i].x=id[A[i].x];
		A[i].y=id[A[i].y];
		
		f[A[i].x][A[i].y]=min(f[A[i].x][A[i].y],A[i].s);
		f[A[i].y][A[i].x]=min(f[A[i].y][A[i].x],A[i].s);
	}
	
	fo(k,1,N)
	{
		fo(i,1,N)
		if (i!=k)
		{
			fo(j,1,N)
			if (j!=i && j!=k && f[i][j]>f[i][k]+f[k][j])
			f[i][j]=f[i][k]+f[k][j];
		}
	}
	
	l=0;
	fo(i,1,N-1)
	{
		fo(j,i+1,N)
		ans[++l]=f[i][j];
	}
	
	sort(ans+1,ans+l+1);
	
	printf("%I64d\n",ans[K]);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值