HDU 3991 Harry Potter and the Present II(Floyd+DAG最小路径覆盖)
来源:互联网 发布:阿里云短信发送 c 编辑:程序博客网 时间:2024/05/29 18:32
HDU 3991 Harry Potter and the Present II(Floyd+DAG最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=3991
题意:
一幅图上有N个点,M条边,边的权值表示通过这条边所需要的时间。有Q个任务,每次任务以Ci,Ti的形式给出Ci表示城市编号,Ti表示任务需要在Ti这个时间点完成.现在你拥有一个人,问至少还需要几个人才能完成所有城市给定时间上的任务。每个人都可以在任何时间点出现在任何城市。
分析:
本题很类似于POJ3216:点击打开链接
我们把每个任务看成一个点,如果我们完成任务i之后还能在任务j开始之前到达j所在的城市,那么就连一条从i到j的有向边。 我们就得到一个DAG图了。
为了要让人员充分利用,一个人送完一个地方的礼物后,如果他还来的及赶到另外一个地方去送礼,那么明显他应该继续去送礼。这就形成了本DAG图的一条简单路径(即单向路径)。
为了要使人员最少且每个礼物都送到,那么我们就要找出尽量少的简单路径(每条路径不相交)且所有简单路径正好覆盖了DAG图的所有顶点。
那么我们总共需要送礼物的人就是本DAG图的最小路径覆盖数。.
那么本题只需要建立二分图,然后用Q 减去二分图最大匹配数 即是总共需要送礼物的人。(不要忘了减去哈利自己哦)
AC代码:
#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=1000+5;#define inf 0x3f3f3f3fstruct Max_Match{ int n,m; vector<int>g[maxn]; bool vis[maxn]; int left[maxn]; void init(int n,int m) { this->n=n; this->m=m; for(int i=1;i<=n;i++)g[i].clear(); memset(left,-1,sizeof(left)); } bool match(int u) { for(int i=0;i<g[u].size();i++){int v=g[u][i];if(!vis[v]){vis[v]=true;if(left[v]==-1 || match(left[v])){left[v]=u;return true;}}} return false; } int solve() { int ans=0; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(match(i)) ans++; } return ans; }}MM;long long maps[200][200];struct node{long long place,start;}s[maxn];void floyd(int m){for(int k=0;k<m;k++)for(int i=0;i<m;i++)for(int j=0;j<m;j++){if(maps[i][k]!=inf&&maps[k][j]!=inf){maps[i][j]=min(maps[i][j],maps[i][k]+maps[k][j]);}}}int main(){int n,m,q;int t;scanf("%d",&t);int cas=1;int u,v; while(t--){//memset(maps,inf,sizeof maps);scanf("%d%d%d",&n,&m,&q);for(int i=0;i<n;i++)for(int j=0;j<n;j++)maps[i][j]=i==j?0:inf;for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);scanf("%lld",&maps[u][v]);maps[v][u]=maps[u][v];}floyd(n);MM.init(q,q);for(int i=1;i<=q;i++)scanf("%d%d",&s[i].place,&s[i].start);for(int i=1;i<=q;i++){for(int j=1;j<=q;j++){if(i!=j&&maps[s[i].place][s[j].place]!=inf){if(s[i].start+maps[s[i].place][s[j].place]<=s[j].start){MM.g[i].push_back(j);}}}}printf("Case %d: %d\n",cas++,q-MM.solve()-1);//减去本人} return 0;}/*22 1 20 1 10 11 22 1 20 1 20 11 2*/
阅读全文
0 0
- HDU 3991 Harry Potter and the Present II(Floyd+DAG最小路径覆盖)
- HDU 3991 Harry Potter and the Present II(Floyd+DAG最小路径覆盖)
- HDU 3991 Harry Potter and the Present II(Floyd+DAG最小路径覆盖)
- HDU--3991[Harry Potter and the Present II] Floyd预处理+最小路径覆盖
- hdu 3991 Harry Potter and the Present II //最小路径覆盖
- hdu 3991 Harry Potter and the Present II (最短路+最小路径覆盖)
- hdu 3991 Harry Potter and the Present II
- hdu 3991 Harry Potter and the Present II
- 最小匹配hdu 3991 Harry Potter and the Present
- hdu3991 Harry Potter and the Present II
- HDU 3987 Harry Potter and the Forbidden Forest 最小割
- 【HDU】3987 Harry Potter and the Forbidden Forest 最小割
- 【最小割】HDU 3987 Harry Potter and the Forbidden Forest
- hdu 3987 Harry Potter and the Forbidden Forest 最小割
- Harry Potter and the Hide Story HDU
- HDU--3987[Harry Potter and the Forbidden Forest] 求最小割集中的最小边数
- hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
- HDU 3987 Harry Potter and the Forbidden Forest (求割边最少的最小割)
- 广播监听网络状态
- 【23种设计模式】结构型模式 > 适配器模式
- UILabel中的文字吸顶(顶部对齐)
- CentOS6.4安装MySQL5.7.12
- POJ
- HDU 3991 Harry Potter and the Present II(Floyd+DAG最小路径覆盖)
- 去除C程序中的注释
- WPF中的Style
- CRC16校验码生成原理
- 【转载】将自建表数据内容包含在传输请求号中(SAP)
- 【jzoj5333】【NOIP2017提高A组模拟8.23】【大新闻】【可持久化线段树】
- Spring MVC原理及配置详解
- 深入机器学习系列1-序章
- 上拉刷新下拉加载