Merry Christmas

Time Limit : 8 sec, Memory Limit : 65536 KB

Problem J: Merry Christmas

International Christmas Present Company (ICPC) is a company to employ Santa and deliver presents on Christmas. Many parents request ICPC to deliver presents to their children at specified time of December 24. Although same Santa can deliver two or more presents, because it takes time to move between houses, two or more Santa might be needed to finish all the requests on time.

Employing Santa needs much money, so the president of ICPC employed you, a great program- mer, to optimize delivery schedule. Your task is to write a program to calculate the minimum number of Santa necessary to finish the given requests on time. Because each Santa has been well trained and can conceal himself in the town, you can put the initial position of each Santa anywhere.

Input

The input consists of several datasets. Each dataset is formatted as follows.

N M L
uvd1
uvd2
.
.
.
uM vM dM
pt1
pt2
.
.
.
pL tL

The first line of a dataset contains three integer, N , M and L (1 ≤ N ≤ 100, 0 ≤ M ≤ 1000, 1 ≤ L ≤ 1000) each indicates the number of houses, roads and requests respectively.

The following M lines describe the road network. The i-th line contains three integers, ui , vi , and di (0 ≤ ui < vi≤ N - 1, 1 ≤ di ≤ 100) which means that there is a road connecting houses ui and vi with di length. Each road is bidirectional. There is at most one road between same pair of houses. Whole network might be disconnected.

The next L lines describe the requests. The i-th line contains two integers, pi and ti (0 ≤ pi ≤ N - 1, 0 ≤ ti ≤ 108 ) which means that there is a delivery request to house pi on time ti . There is at most one request for same place and time. You can assume that time taken other than movement can be neglectable, and every Santa has the same speed, one unit distance per unit time.

The end of the input is indicated by a line containing three zeros separated by a space, and you should not process this as a test case.

Output

Print the minimum number of Santa necessary to finish all the requests on time.

Sample Input

  1. 3 2 3
  2. 0 1 10
  3. 1 2 10
  4. 0 0
  5. 1 10
  6. 2 0
  7. 3 2 4
  8. 0 1 10
  9. 1 2 10
  10. 0 0
  11. 1 10
  12. 2 20
  13. 0 40
  14. 10 10 10
  15. 0 1 39
  16. 2 3 48
  17. 3 5 20
  18. 4 8 43
  19. 3 9 10
  20. 8 9 40
  21. 3 4 5
  22. 5 7 20
  23. 1 7 93
  24. 1 3 20
  25. 0 0
  26. 1 100000000
  27. 2 100
  28. 3 543
  29. 4 500
  30. 5 400
  31. 6 300
  32. 7 200
  33. 8 100
  34. 9 100
  35. 0 0 0

Output for the Sample Input

  1. 2
  2. 1
  3. 4

题意:有L个货物需要圣诞老人派送,每个货物要送到谁家,并且几点之前送掉都有严格规定。准时送完全部的L个货物至少需要多少个圣诞老人。

思路:一开始我们考虑最坏的情况,即L个货物需要L个圣诞老人才能全部送完,之后再进行优化,找找是否可以少用圣诞来人。如果一个圣诞老人送完一个货物后有足够时间走到另外一个货物的送货地点,那么这两个货物可以由这一个圣诞老人一起送,这样就能少用一个圣诞老人。把所有能够一起送的两个货物之间都连上边,图的最大匹配即为能减少的圣诞老人个数x,L-x就是最少所需圣诞老人数。例如如下情况二分图:
                                     即为货物1送完可直接送货物2;货物2送完可直接送货物3;货物4送完可直接送货物5,货物5送完可直接送货物3,其最大匹配为3,那么一共可以节省3个圣诞老人。

代码:

  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include<iostream>
  3. #include<stdio.h>
  4. #include<algorithm>
  5. #include<queue>
  6. #include<set>
  7. #include<vector>
  8. #include<cstring>
  9. #include<string>
  10. using namespace std;
  11. #define INF 0x3f3f3f3f
  12. const int N_MAX = + , V_MAX = + ;
  13. int V;//点的个数
  14. vector<int>G[N_MAX];
  15. int match[N_MAX];
  16. bool used[N_MAX];
  17. void add_edge(int u, int v) {
  18. G[u].push_back(v);
  19. G[v].push_back(u);
  20. }
  21.  
  22. bool dfs(int v) {
  23. used[v] = true;
  24. for (int i = ; i < G[v].size(); i++) {
  25. int u = G[v][i], w = match[u];
  26. if (w < || !used[w] && dfs(w)) {
  27. match[v] = u;
  28. match[u] = v;
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34.  
  35. int bipartite_matching() {
  36. int res = ;
  37. memset(match, -, sizeof(match));
  38. for (int v = ; v < V; v++) {
  39. if (match[v] < ) {
  40. memset(used, , sizeof(used));
  41. if (dfs(v))
  42. res++;
  43. }
  44. }
  45. return res;
  46. }
  47. int N, M, L, dis[N_MAX][N_MAX];
  48. int p[N_MAX], t[N_MAX];
  49. int main() {
  50.  
  51. while (scanf("%d%d%d", &N, &M, &L) && N) {
  52. V = * L;
  53. memset(dis, INF, sizeof(dis));
  54. for (int i = ; i < M; i++) {
  55. int u, v, d;
  56. scanf("%d%d%d", &u, &v, &d);
  57. dis[u][v] = d;
  58. dis[v][u] = d;
  59. }
  60. for (int i = ; i < L; i++) {
  61. scanf("%d%d", p + i, t + i);
  62. }
  63. //floyd
  64. for (int k = ; k < N; k++) {
  65. dis[k][k] = ;//!!!!!!!!
  66. for (int i = ; i < N; i++)
  67. for (int j = ; j < N; j++) {
  68. dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
  69. }
  70. }
  71. /////
  72. for (int i = ; i < L; i++) {
  73. for (int j = ; j < L; j++) {
  74. if (i != j&&t[i] + dis[p[i]][p[j]] <= t[j]) {//可以从p[i]走到p[j]送货,少用一个人
  75. add_edge( i, L+j);//连一条边,连边的两端货物可以让一个人送即可
  76. }
  77. }
  78. }
  79. printf("%d\n", L - bipartite_matching());
  80. for (int i = ; i < V; i++)
  81. G[i].clear();
  82. }
  83.  
  84. return ;
  85. }

aoj 2226 Merry Christmas的更多相关文章

  1. AOJ 2251 Merry Christmas (最小点覆盖)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251 [题目大意] 给出一张图,现在有一些任务,要求在ti时刻送礼物 ...

  2. shit antd & Merry Christmas bug

    shit antd & Merry Christmas bug https://github.com/ant-design/ant-design/issues/13098 antd 玩大了? ...

  3. 《一头扎进》系列之Python+Selenium框架实战篇7 - 年底升职加薪,年终奖全靠它!Merry Christmas

    1. 简介 截止到上一篇文章为止,框架基本完全搭建完成.那么今天我们要做什么呢????聪明如你的小伙伴或者是童鞋一定已经猜到了,都测试完了,当然是要生成一份高端大气上档次的测试报告了.没错的,今天宏哥 ...

  4. 我的平安夜-Merry Christmas

    我的平安夜-Merry Christmas 平安夜给自己买的第一个"苹果",嘻嘻. 今夜,不想去学习技术知识点什么的, 我们就想到哪里写哪里,就简单聊聊思维方式吧. 其实我不想做今 ...

  5. Merry Christmas & Happy New Year!!

    圣诞快乐,新年快乐!

  6. merry Christmas

    圣诞节的来历 圣诞节这个名称是基督弥撒的缩写. 弥撒是教会的一种礼拜仪式. 1.耶诞节是一个宗节,我们把它当作耶苏的诞辰来庆祝,因而又名耶诞节.这一天,世界所有的基督教会都举行特别的礼拜仪式.但是有很 ...

  7. Merry Christmas 2015

    祝大家圣诞快乐! 昨天下班在电梯里遇见HR大BOSS,她说公司今天上午有2200个员工要带小孩子来参加Children's Holidy Party...我问了句,那是不是有免费早餐和午餐啊,她说 & ...

  8. pandaboy Merry Christmas

  9. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

随机推荐

  1. PAT (Basic Level) Practise (中文)- 1012. 数字分类 (20)

    http://www.patest.cn/contests/pat-b-practise/1012 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数 ...

  2. PAT (Basic Level) Practise (中文)- 1011. A+B和C (15)

    http://www.patest.cn/contests/pat-b-practise/1011 给定区间[-231, 231]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1 ...

  3. java基础—泛型

    一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: 1 package cn.gacl.generic.summary; 2 3 import jav ...

  4. ovx openVirtex的阅读文档

    由于flowvisor只有4个版本, 最新更新都是2013年的, 跟底层ovs版本不跟进, 最近斯坦福post一个 ovx, 猜测是flowvisor的加强版, 所以看一下文档说明 文档详见http: ...

  5. Mybatis学习记录(3)

    1.输出映射和输入映射 Mapper.xml映射文件定义了操作数据库的sql,每个sql就是一个statement,映射文件是mybatis的核心. (1)parameterType(输入类型)   ...

  6. 个人对spring的IOC+DI的封装

    暂时支持8种基本数据类型,String类型,引用类型,List的注入. 核心代码 package day01; import java.lang.reflect.Field;import java.l ...

  7. mysql crash cource 书中实例

    样例表 CREATE TABLE customers(  cust_id      int       NOT NULL AUTO_INCREMENT,  cust_name    char(50)  ...

  8. java String中的replace(oldChar,newChar) replace(CharSequence target,CharSequence replacement) replaceAll replaceFirst 面试题:输入英文语句,单词首字符大写后输出 char String int 相互转换

    package com.swift; import java.util.Scanner; public class FirstChat_ToCaps_Test { public static void ...

  9. Diff Two Arrays-freecodecamp算法题目

    Diff Two Arrays(比较两个数组) 1.要求 比较两个数组,然后返回一个新数组 该数组的元素为两个给定数组中所有独有的数组元素.换言之,返回两个数组的差异. 2.思路 定义一个新数组变量, ...

  10. ubuntu 16.04 +anaconda3.6 +Nvidia DRIVER 390.77 +CUDA9.0 +cudnn7.0.4+tensorflow1.5.0+neural-style

    这是我第一个人工智能实验.虽然原理不是很懂,但是觉得深度学习真的很有趣.教程如下. Table of Contents 配置 时间轴 前期准备工作 anaconda3 安装 bug 1:conda:未 ...