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. Effective C# 学习笔记(原则一:始终能的使用属性(property),而不是可直接访问的Data Member)

    原则一:始终能的使用属性(property),而不是可直接访问的Data Member    Always use properties instead of accessible data memb ...

  2. eval和new Function的区别

    eval和new Function都可以动态解析和执行字符串.但是它们对解析内容的运行环境判定不同. var a = 'global scope' function b(){ var a = 'loc ...

  3. .NET开源工作流RoadFlow-表单设计-文本框

    点击表单设计器工具栏上的文本框按钮,会弹出文本框属性对话框: 绑定字段:该文本框与表单属性设置中选择的表的某个字段绑定(该文本框中的值将会保存到该字段中). 默认值:该文本框的初始化值. 宽度:文本框 ...

  4. android开发系列之gradle认识

    后面的系列博客,我将会写一写自己这段时间对于android的学习.认识.体会,希望能够与大家分享. 相信大家从ADT开发切换到android studio最大.最直观的变化就是gradle,因为在an ...

  5. 第三方登录开发-Facebook

    这次这个项目要分别可以使用新浪微博,qq互联以及Facebook和Twitter授权登录 facebook目前只支持oauth2技术,个人理解其工作流程是当用户想访问当前网站,却不想注册账号,此时当前 ...

  6. 27.some company's Spi Flash chip replace altera epcsxxx

    由于altera公司的epcsxxx芯片比较贵,所以一般用其它公司的spi flash芯片代替也可以.据AlteraFAE描述:“EPCS器件也是选用某家公司的SPIFlash,只是中间经过Alter ...

  7. Hibernate从入门到精通(十)多对多单向关联映射

    上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...

  8. 怎样把php数组转换成字符串,php implode()

    实例代码 一维数组转换成字符串代码! <?php $arr1=array("shu","zhu","1"); $c=implode(& ...

  9. 用PHP对数据库内容进行操作(改)

    查询页面(用户可见) <body> <table width="80%" border="1" cellpadding="0&quo ...

  10. JS正则表达式 替换括号,尖括号等

    function toTxt(str) { var RexStr = /\<|\>|\"|\'|\&/g str = str.replace(RexStr, functi ...