1 条题解

  • 0
    @ 2025-4-6 22:07:58

    题意:从1到n,要走不少于K条路,每条路不能重复走,点可以重复走,求所走的路中最长的最小,,

    找出距离的最大值,然后二分,建图时符合的每条边链接的两点间建双向边,流量都为1,求最大流

    #include<stdio.h>
    #include<string.h>
    const int N=210;
    const int inf=0x3fffffff;
    int dis[N],gap[N],head[N],num,start,end,ans,n,m;
    struct edge
    {
    	int st,ed,flow,next;
    }E[200000];
    struct node
    {
    	int x,y,w;
    }P[40000];
    void addedge(int x,int y,int w)
    {
    	E[num].st=x;E[num].ed=y;E[num].flow=w;E[num].next=head[x];head[x]=num++;
    	E[num].st=y;E[num].ed=x;E[num].flow=w;E[num].next=head[y];head[y]=num++;
    }
    void makemap(int D)
    {
    	memset(head,-1,sizeof(head));
    	num=0;
    	int i;
    	for(i=0;i<m;i++)
    	{
    		if(P[i].w<=D)
    			addedge(P[i].x,P[i].y,1);
    	}
    }
    int dfs(int u,int minflow)
    {
    	if(u==end)return minflow;
    	int i,v,f,flow=0,min_dis=ans-1;
    	for(i=head[u];i!=-1;i=E[i].next)
    	{
    		if(E[i].flow)
    		{
    			v=E[i].ed;
    			if(dis[v]+1==dis[u])
    			{
    				f=dfs(v,E[i].flow>minflow-flow?minflow-flow:E[i].flow);
    				E[i].flow-=f;
    				E[i^1].flow+=f;
    				flow+=f;
    				if(flow==minflow)break;
    				if(dis[start]>=ans)return flow;
    			}
    			min_dis=min_dis>dis[v]?dis[v]:min_dis;
    		}
    	}
    	if(flow==0)
    	{
    		if(--gap[dis[u]]==0)
    			dis[start]=ans;
    		dis[u]=min_dis+1;
    		gap[dis[u]]++;
    	}
    	return flow;
    }
    int isap()
    {
    	int maxflow=0;
    	memset(dis,0,sizeof(dis));
    	memset(gap,0,sizeof(gap));
    	gap[0]=ans;
    	while(dis[start]<ans)
    	   maxflow+=dfs(start,inf);
    	return maxflow;
    }
    int main()
    {
    	int i,k,L,R,mid;
    	while(scanf("%d%d%d",&n,&m,&k)!=-1)
    	{
    		R=0;start=1;end=n;ans=n;
    		for(i=0;i<m;i++)
    		{
    			scanf("%d%d%d",&P[i].x,&P[i].y,&P[i].w);
    			if(R<P[i].w)R=P[i].w;
    		}
    		L=0;
    		while(L<R)
    		{
               mid=(L+R)/2;
    		   makemap(mid);
    		   if(isap()>=k)
    			   R=mid;
    		   else L=mid+1;
    		}
    		printf("%d\n",R);
    	}
    	return 0;
    }
     
    
    • 1

    信息

    ID
    1457
    时间
    1000ms
    内存
    64MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者