您的位置:首页 > 大数据 > 人工智能

hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)

2016-08-08 16:11 337 查看

Delay Constrained Maximum Capacity Path

Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1790 Accepted Submission(s):
577


[align=left]Problem Description[/align]
Consider an undirected graph with N vertices, numbered
from 1 to N, and M edges. The vertex numbered with 1 corresponds to a mine from
where some precious minerals are extracted. The vertex numbered with N
corresponds to a minerals processing factory. Each edge has an associated travel
time (in time units) and capacity (in units of minerals). It has been decided
that the minerals which are extracted from the mine will be delivered to the
factory using a single path. This path should have the highest capacity
possible, in order to be able to transport simultaneously as many units of
minerals as possible. The capacity of a path is equal to the smallest capacity
of any of its edges. However, the minerals are very sensitive and, once
extracted from the mine, they will start decomposing after T time units, unless
they reach the factory within this time interval. Therefore, the total travel
time of the chosen path (the sum of the travel times of its edges) should be
less or equal to T.

[align=left]Input[/align]
The first line of input contains an integer number X,
representing the number of test cases to follow. The first line of each test
case contains 3 integer numbers, separated by blanks: N (2 <= N <=
10.000), M (1 <= M <= 50.000) and T (1 <= T <= 500.000). Each of the
next M lines will contain four integer numbers each, separated by blanks: A, B,
C and D, meaning that there is an edge between vertices A and B, having capacity
C (1 <= C <= 2.000.000.000) and the travel time D (1 <= D <=
50.000). A and B are different integers between 1 and N. There will exist at
most one edge between any two vertices.

[align=left]Output[/align]
For each of the X test cases, in the order given in the
input, print one line containing the highest capacity of a path from the mine to
the factory, considering the travel time constraint. There will always exist at
least one path between the mine and the factory obbeying the travel time
constraint.

[align=left]Sample Input[/align]

2
2 1 10

1 2 13 10
4 4 20

1 2 1000 15

2 4 999 6

1 3 100 15

3 4 99 4

[align=left]Sample Output[/align]

13
99

[align=left]Author[/align]
Mugurel Ionut Andreica

[align=left]题意:有N个点,点1为珍贵矿物的采矿区, 点N为加工厂,有M条双向连通的边连接这些点。走每条边的运输容量为C,运送时间为D。他们要选择一条从1到N的路径运输, 这条路径的运输总时间要在T之内,在这个前提之下,要让这条路径的运输容量尽可能地大。每条路径的运输容量取决与这条路径中的运输容量最小的那条边。[/align]
[align=left] [/align]
[align=left]使用二分枚举,因为资源越多,能走的路就越少,因此使用二分从大到小排序之后枚举。[/align]
[align=left] [/align]
[align=left]附上代码:[/align]
[align=left] [/align]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
#define M 50005
#define N 10005
using namespace std;

int tol,n,m,t,limit;
struct Edge
{
int form,to,val,time;
int next;
} edge[M*2];

int head[M*2],dis
,r[M];
bool vis
;

bool cmp(int a,int b)
{
return a>b;
}

void init()
{
tol=0;
memset(head,-1,sizeof(head));
}

void addEdge(int u,int v,int val,int time) ///邻接表
{
edge[tol].form=u;
edge[tol].to=v;
edge[tol].val=val;
edge[tol].time=time;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].form=v;
edge[tol].to=u;
edge[tol].val=val;
edge[tol].time=time;
edge[tol].next=head[v];
head[v]=tol++;
}

void getmap()
{
scanf("%d%d%d",&n,&m,&t);
int a,b,c,d;
for(int i=0; i<m; i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
r[i]=c;
addEdge(a,b,c,d);
}
sort(r,r+m,cmp); ///从大到小排序

}

int spfa() ///求最短时间
{
memset(dis,INF,sizeof(dis));
memset(vis,false,sizeof(vis));
queue<int>q;
q.push(1);
dis[1]=0;
vis[1]=true;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].val>=limit)
if(dis[v]>dis[u]+edge[i].time)
{
dis[v]=dis[u]+edge[i].time;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
return dis
;
}

void search()
{
int left=0,right=m-1,mid;
while(left<right) ///二分
{
mid=(left+right)/2;
limit=r[mid];
int tmp=spfa();
if(tmp==INF||tmp>t)
left=mid+1;
else
right=mid;
}
printf("%d\n",r[left]);
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
getmap();
search();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: