HDU-1839 Delay Constrained Maximum Capacity Path 最短路+二分

题目链接

类似题:HDU-2962

题目大意:
有N个点,点1为珍贵矿物的采矿区, 点N为加工厂,有M条双向连通的边连接这些点。走每条边的运输容量为C,运送时间为D。
他们要选择一条从1到N的路径运输, 这条路径的运输总时间要在T之内,在这个前提之下,要让这条路径的运输容量尽可能地大。
一条路径的运输容量取决与这条路径中的运输容量最小的那条边。

解题思路:

二分查找路径中最小容量 最短路看是否满足在T时间内到达 


#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 10005;
const int inf = 1<<30;
typedef __int64 LL;
int n,m,T,pos;
struct node
{
	int to,w,cap,next;
}edge[maxn*10];
int head[maxn],cap[maxn*10],dis[maxn];
bool vis[maxn];
void addEdge( int u,int v,int c,int w )
{
	edge[++pos].to = v;	edge[pos].w = w; edge[pos].cap = c;
	edge[pos].next = head[u];	head[u] = pos;
}
typedef pair<int,int> pii;
bool Dijkstra( int s,int maxCap )
{
	priority_queue<pii,vector<pii>,greater<pii> >que;
	memset( vis,0,sizeof(vis) );
	for( int i = 1; i <= n; i ++ )	dis[i] = inf;
	dis[s] = 0;
	que.push( make_pair(dis[s],s) );
	while( !que.empty() ){
		pii u = que.top();	que.pop();
		int x = u.second;
		if( dis[x] > T ) return false;
		if( x == n )	return true;
		if( vis[x] )	continue;
		vis[x] = true;
		for( int i = head[x]; i != -1; i = edge[i].next ){
			node &v = edge[i];
			if( v.cap < maxCap )	continue;
			if( dis[v.to] > dis[x] + v.w ){
				dis[v.to] = dis[x] + v.w;
				que.push( make_pair(dis[v.to],v.to) );
			}
		}
	}
	return false;
}
void fun()
{
	sort( cap,cap+m );
	int ld = 0,rd = 1,mid;
	for( int i = 1; i < m; i ++ )
		if( cap[i] != cap[i-1] )	cap[rd++] = cap[i];
	while( ld < rd )
	{
		mid = ( ld + rd )>>1;
		if( Dijkstra( 1,cap[mid] ))
			ld = mid + 1;
		else
			rd = mid;
	}
	printf("%d\n",cap[ld-1]);
}
int main()
{
    //freopen("data.txt","r",stdin);
	int cas,u,v,c,w;
	scanf("%d",&cas);
    while( cas -- ){
		pos = 0;
		scanf("%d%d%d",&n,&m,&T);
		for( int i = 0; i <= n; i ++ )
			head[i] = -1;
		for( int i = 0; i < m; i ++ ){
			scanf("%d%d%d%d",&u,&v,&c,&w);
			addEdge( u,v,c,w );
			addEdge( v,u,c,w );
			cap[i] = c;
		}
		fun();
	}
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值