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

3 2 3
0 1 10
1 2 10
0 0
1 10
2 0
3 2 4
0 1 10
1 2 10
0 0
1 10
2 20
0 40
10 10 10
0 1 39
2 3 48
3 5 20
4 8 43
3 9 10
8 9 40
3 4 5
5 7 20
1 7 93
1 3 20
0 0
1 100000000
2 100
3 543
4 500
5 400
6 300
7 200
8 100
9 100
0 0 0

Output for the Sample Input

2
1
4
无法直视,去年我做这题怎么会WA十几次,水题!!!! 走一次floyd ,每个询问都当作一个点,做最小路径覆盖即可
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; #define maxn 105
#define INF (1 << 30) int n,m,l;
int dis[maxn][maxn],p[],t[];
int match[];
bool vis[];
vector<int> G[]; void floyd() {
for(int k = ; k < n; k++) {
for(int i = ; i < n; ++i) {
for(int j = ; j < n; ++j) {
if(dis[i][k] != INF && dis[k][j] != INF) {
dis[i][j] = min(dis[i][j],
dis[i][k] + dis[k][j]);
} }
}
}
} bool dfs(int u) {
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(vis[v]) continue;
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true;
} } return false;
} void build() {
for(int i = ; i <= l; ++i) {
for(int j = ; j <= l; ++j) {
if(i == j) continue;
if(dis[ p[i] ][ p[j] ] != INF &&
dis[ p[i] ][ p[j] ] + t[i] <= t[j]) { G[i].push_back(j);
}
}
}
} void solve() {
floyd(); build(); int ans = ; for(int i = ; i <= l; ++i) match[i] = -; for(int i = ; i <= l; ++i) {
memset(vis,,sizeof(vis));
if(dfs(i)) ++ans;
} // printf("ans = %d\n",ans); printf("%d\n",l - ans); } int main()
{
//freopen("sw.in","r",stdin); while(~scanf("%d%d%d",&n,&m,&l)) { if(!n && !m && !l) break; for(int i = ; i < n; ++i) {
for(int j = ; j < n; ++j) {
dis[i][j] = i == j ? : INF;
}
} for(int i = ; i <= l; ++i) G[i].clear(); for(int i = ; i <= m; ++i) {
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
dis[u][v] = dis[v][u] = w;
} for(int i = ; i <= l; ++i) {
scanf("%d%d",&p[i],&t[i]);
} solve(); } return ;
}

AIZU 2251的更多相关文章

  1. [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)

    对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...

  2. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  3. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. poj 2251

    http://poj.org/problem?id=2251 一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向. 题意就是从S走到E,#不能走. #include < ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  7. bzoj 2251: [2010Beijing Wc]外星联络 后缀数组

    2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 424  Solved: 232[Submit][ ...

  8. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  9. ●BZOJ 2251 [2010Beijing Wc]外星联络

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2251 题解: 后缀数组,倍增,RMQ 题意:把重复次数超过 1次的子串按字典序输出它们重复的 ...

随机推荐

  1. angularjs2 学习笔记(五) http服务

    angular2的http服务是用于从后台程序获取或更新数据的一种机制,通常情况我们需要将与后台交换数据的模块做出angular服务,利用http获取更新后台数据,angular使用http的get或 ...

  2. JavaWeb之Servlet: ServletConfig 与 ServletContext

    ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...

  3. String.split()方法你可能不知道的一面

    一.问题 java中String的split()是我们经常使用的方法,用来按照特定字符分割字符串,那么我们看以下一段代码: public void splitTest() { String str = ...

  4. [iOS]MVVM-框架介绍

       我于 2011 年在 500px 找到自己的第一份 iOS 开发工作.虽然我已经在大学里做了好几年 iOS 外包开发,但这才是我的一个真正的 iOS 开发工作.我被作为唯一的 iOS 开发者被招 ...

  5. ext4.1动态生成多个checkboxgroup(或者radiogroup),并且有toolbar操作、

    转载自:http://blog.csdn.net/zhengyuechuan/article/details/9327291 前台controller代码: Ext.define('zyc.contr ...

  6. 安装MSITVPN连接的时候弹出:需要(未知)上的文件'MSITVPN.bmp。

    使用 msitvpn 连接microsoft 公司内网,在安装msitvpn的时候突然弹出一个对话框提示需要msitvpn.bmp 文件,找了很久都没找到解决问题方案. 最后只能猜测是不是和用户的权限 ...

  7. Postgresql 存储过程调试 1

    看来人真的有些力不从心,半个月前还很得意掌握的简单的Postgresql 存储过程的调试,一段时间没使用,做新功能就忘了! Postgresql 在开源的数据库里面算是很强悍的了,但现在就是不方便调试 ...

  8. apache-activemq-5.14.0学习总结

    一.什么是ActiveMQ? 百度百科对ActiveMQ的描述:

  9. STL之multiset

    参见http://www.cplusplus.com/reference/set/multiset/ template < class T,                            ...

  10. java面试资料总结

    一.java基础部分 1.一个“.java”源文件中是否可以包含多个类(不是内部类)?有什么限制? 可以,但是只能有一个public类,且类名与文件名相同 2.java有没有goto? goto是ja ...