Hubtown

时间限制: 10 Sec  内存限制: 256 MB

题目描述

Hubtown is a large Nordic city which is home to n citizens. Every morning, each of its citizens wants to travel to the central hub from which the city gets its name, by using one of the m commuter trains which pass through the city. Each train line is a ray (i.e., a line segment which extends infinitely long in one direction), ending at the central hub, which is located at coordinates (0, 0). However, the train lines have limited capacity (which may vary between train lines), so some train lines may become full, leading to citizens taking their cars instead of commuting. The city council wishes to minimize the number of people who go by car. In order to do this, they will issue instructions stating which citizens are allowed to take which train. 
A citizen will always take the train line which is of least angular distance from its house. However, if a citizen is exactly in the middle between two train lines, they are willing to take either of them, and city council can decide which of the two train lines the citizen should use. 
See Figure H.1 for an example.

Figure H.1: Illustration of Sample Input 1. The dashed arrows indicate which train lines the citizens are closest to (note that we are measuring angular distances, not Euclidean distance).
Your task is to help the council, by finding a maximum size subset of citizens who can go by train in the morning to the central hub, ensuring that each of the citizens take one of the lines they are closest to, while not exceeding the capacity of any train line. For this subset, you should also print what train they are to take.

输入

The first line of input contains two integers n and m, where 0 ≤ n ≤ 200 000 is the number of citizens, and 1 ≤ m ≤ 200 000 is the number of train lines.
The next n lines each contain two integers x and y, the Cartesian coordinates of a citizen’s home. No citizen lives at the central hub of the city.
Then follow m lines, each containing three integers x, y, and c describing a train line, where (x, y) are the coordinates of a single point (distinct from the central hub of the city) which the train line passes through and 0 ≤ c ≤ n is the capacity of the train line. The train line is the ray starting at (0, 0) and passing through (x, y).
All coordinates x and y (both citizens’ homes and the points defining the train lines) are bounded by 1000 in absolute value. No two train lines overlap, but multiple citizens may live at the same coordinates.

输出

First, output a single integer s – the maximum number of citizens who can go by train. Then,output s lines, one for each citizen that goes by train. On each line, output the index of the citizen followed by the index of the train line the citizen takes. The indices should be zero-indexed (i.e.,between 0 and n − 1 for citizens, and between 0 and m − 1 for train lines, respectively), using the same order as they were given in the input.

样例输入

3 2
2 0
-1 0
-2 -1
1 -1 1
1 1 2

样例输出

3
0 1
1 1
2 0

 

题意:n个人,m个铁轨,每个人要到最近的铁轨去,若最近的有两个可二选一,每个铁轨能承受的人数有限,问最多多少个人可以到铁轨上。

做法:先对人和铁轨一起进行极角排序,然后记录一下距离人最近的上下两个铁轨,之后建图跑最大流。具体细节在代码中说明。

此外,这题能跑最大流是因为网络流跑二分图匹配的时间复杂度是 O(m*sqrt(n)),而且实际编程中速度会更快。

#include<bits/stdc++.h>
#define N 400050
#define M 2000050
using namespace std;
typedef struct
{
int v;
int flow;
} ss; ss edg[M];
vector<int>edges[N];
int now_edges=; void addedge(int u,int v,int flow)
{
// printf(" %d %d %d\n",u,v,flow);
edges[u].push_back(now_edges);
edg[now_edges++]=(ss)
{
v,flow
};
edges[v].push_back(now_edges);
edg[now_edges++]=(ss)
{
u,
};
} int dis[N],S,T;
bool bfs()
{
memset(dis,,sizeof(dis));
queue<int>q;
q.push(S);
dis[S]=; while(!q.empty())
{
int now=q.front();
q.pop();
int Size=edges[now].size(); for(int i=; i<Size; i++)
{
ss e=edg[edges[now][i]];
if(e.flow>&&dis[e.v]==)
{
dis[e.v]=dis[now]+;
q.push(e.v);
}
}
}
if(dis[T]==)
return ;
return ; }
int current[N];
int dfs(int now,int maxflow)
{
if(now==T)
return maxflow;
int Size=edges[now].size();
for(int i=current[now]; i<Size; i++)
{
current[now]=i;
ss &e=edg[edges[now][i]]; if(e.flow>&&dis[e.v]==dis[now]+)
{
int Flow=dfs(e.v,min(maxflow,e.flow)); if(Flow)
{
e.flow-=Flow;
edg[edges[now][i]^].flow+=Flow;
return Flow;
}
}
}
return ;
} int dinic()
{
int ans=,flow;
while(bfs())
{
memset(current,,sizeof(current));
while(flow=dfs(S,INT_MAX/))
ans+=flow;
}
return ans;
} struct orz //铁轨和人的统一结构体,value<0为人,value>0为铁轨
{
int value,number;
int x,y,sgn; void setxy(int a,int b)
{
x=a;
y=b;
if(!x)sgn=y>;
else sgn=x>;
}
}; int cross(int x1,int y1,int x2,int y2)//计算叉积
{
return (x1*y2-x2*y1);
} int compare(orz a,orz b,orz c)//计算极角
{
return cross((b.x-a.x),(b.y-a.y),(c.x-a.x),(c.y-a.y));
} bool cmp(orz a,orz b)
{
if(a.sgn!=b.sgn)return a.sgn<b.sgn;
orz c;//原点
c.x = ;
c.y = ;
if(compare(c,a,b)==)//计算叉积,函数在上面有介绍,如果叉积相等,按照X从小到大排序
return a.number>b.number;
else
return compare(c,a,b)<;
} bool point_on_line(orz a,orz b)
{
int d1=__gcd(abs(a.x),abs(a.y)),d2=__gcd(abs(b.x),abs(b.y));
return (a.x/d1==b.x/d2)&&(a.y/d1==b.y/d2);
} const long double epsss=1e-; struct Point
{
int x,y;
Point() {}
Point(int _x,int _y)
{
x=_x,y=_y;
}
};
struct Pointd
{
long double x,y;
Pointd() {}
Pointd(long double _x,long double _y)
{
x=_x,y=_y;
}
}; int cross(const Point&a,const Point&b)
{
return a.x*b.y-a.y*b.x;
} long double crossd(const Pointd&a,const Pointd&b)
{
return a.x*b.y-a.y*b.x;
} int sig(int x)
{
if(x==)
return ;
return x>?:-;
} int sigd(long double x)
{
if(fabs(x)<epsss)
return ;
return x>?:-;
} int distance_cmp(const orz&_a,const orz&_b,const orz&_c)//判断点a距离哪一条射线近
{
Point a(_a.x,_a.y);
Point b(_b.x,_b.y);
Point c(_c.x,_c.y);
Point d;
if(!cross(b,c))
{
d=Point(-b.y,b.x);
if(!cross(a,d))
return ;
if(sig(cross(d,a))==sig(cross(d,b)))
return -;
return ;
}
long double L=sqrt(b.x*b.x+b.y*b.y);
long double R=sqrt(c.x*c.x+c.y*c.y);
Pointd aa(a.x,a.y);
Pointd bb(b.x,b.y);
Pointd cc(c.x,c.y);
Pointd dd(d.x,d.y);
bb.x*=R;
bb.y*=R;
cc.x*=L;
cc.y*=L;
dd=Pointd(bb.x+cc.x,bb.y+cc.y);
if(!sigd(crossd(aa,dd)))
return ;
if(sigd(crossd(dd,aa))==sigd(crossd(dd,bb)))
return -;
return ;
} orz allpoint[N*];
int up[N],down[N]; int main()
{
int n,m;
scanf("%d %d",&n,&m);
S=n+m+;
T=n+m+; for(int i=; i<=n; i++)
{
int x,y;
scanf("%d %d",&x,&y);
allpoint[i].setxy(x,y);
allpoint[i].value=-;
allpoint[i].number=i;
addedge(S,i,);
} for(int i=; i<=m; i++)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
allpoint[i+n].setxy(x,y);
allpoint[i+n].value=z;
allpoint[i+n].number=i+n;
addedge(i+n,T,z);
} sort(allpoint+,allpoint++n+m,cmp);//对人和铁轨一起进行极角排序 for(int i=n+m;i>=;i--)if(allpoint[i].value>=){down[]=i;break;} //寻找最后一个铁轨
for(int i=;i<=n+m;i++)
{
down[i]=down[i-];
if(allpoint[i].value>=)down[i]=i;
} for(int i=;i<=n+m;i++)if(allpoint[i].value>=){up[n+m+]=i;break;}//寻找第一个铁轨
for(int i=n+m;i>=;i--)
{
up[i]=up[i+];
if(allpoint[i].value>=)up[i]=i;
} for(int i=;i<=n+m;i++)
if(allpoint[i].value<)
{
int a=up[i],b=down[i]; if(a==b)addedge(allpoint[i].number,allpoint[a].number,);
else
if(point_on_line(allpoint[i],allpoint[a]))addedge(allpoint[i].number,allpoint[a].number,);
else
if(point_on_line(allpoint[i],allpoint[b]))addedge(allpoint[i].number,allpoint[b].number,);
else
{
int t=distance_cmp(allpoint[i],allpoint[a],allpoint[b]);
if(t<=)addedge(allpoint[i].number,allpoint[a].number,);
if(t>=)addedge(allpoint[i].number,allpoint[b].number,);
}
} int sum=dinic();
printf("%d\n",sum);
for(int i=; i<=n; i++)
{
int Size=edges[i].size();
for(int j=; j<Size; j++)
{
if(edg[edges[i][j]^].flow&&edg[edges[i][j]].v!=S)//注意这里要判一下另一个点是不是起点
{
printf("%d %d\n",i-,edg[edges[i][j]].v-n-);
break;
}
}
}
return ;
}
 

Hubtown的更多相关文章

  1. Hubtown(最大流)

    Hubtown 时间限制: 1 Sec  内存限制: 128 MB提交: 23  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Hubtown is a large N ...

  2. 2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017)

    A. Airport Coffee 设$f_i$表示考虑前$i$个咖啡厅,且在$i$处买咖啡的最小时间,通过单调队列优化转移. 时间复杂度$O(n)$. #include<cstdio> ...

  3. 2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) Solution

    A - Airport Coffee 留坑. B - Best Relay Team 枚举首棒 #include <bits/stdc++.h> using namespace std; ...

随机推荐

  1. codevs 1115 开心的金明

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房 ...

  2. 最完整的台达PLC培训教程(沈阳工大)学习笔记1

    1) 可编程控制器的应用1 开关量逻辑控制:电动机启动与停止2 运动控制:对步进电动机或伺服电动机的单轴或多轴系统实现位置控制3 过程控制:对温度.压力.流量等连续变化的模拟量进行闭环控制4 数据处理 ...

  3. spark 之主成分分析

    C4∗2

  4. Xcode5 如何添加一个Github/Repository 并且Checkout

    1. 添加一个Account  也就是添加一个 Repository. In Xcode, choose Xcode > Preferences, and click Accounts. Pre ...

  5. python中return和yield

    def wx(): a = 'wx' b = '无邪' return a, b print(wx()) print(type(wx())) -----------执行结果--------------- ...

  6. CAD交互绘制mcdbsolid对象(网页版)

    主要用到函数说明: _DMxDrawX::DrawSolid 绘McDbSolid对象.详细说明如下: 参数 说明 DOUBLE dX1 第一个点X DOUBLE dY1 第一个点Y DOUBLE d ...

  7. bootstrap下拉菜单(Dropdowns)

    本章将重点讲解bootstrap下拉菜单(Dropdowns),下拉菜单是可切换的,是以列表格式显示链接的上下文菜单. <!DOCTYPE html><html><hea ...

  8. OpenCV2.4.11+VS2012的环境配置+“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决

    本来OpenCV环境配置的问题是个基础问题,但是步骤有点小烦,所以几乎每次都要百度一下,加上这次遇到的“fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突 ...

  9. UVa-12096-集合栈计算机

    这题的话,我们读入操作之后,首先对于空集就是初始化为空. 我们可以使用typedef 对于 set 重命名为Set,这样就可以直接用Set()的语法进行空集的初始化了. 这题主要是对于集合的处理,我们 ...

  10. MySQL 查询优化之 Multi-Range Read

    MySQL 查询优化之 Multi-Range Read MRR的工作原理 MRR开启与关闭 使用MRR示例 参考文档 在存储引擎中未缓存的大表,使用辅助索引的range scan检索数据, 可能会导 ...