AIZU 2251
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
u1 v1 d1
u2 v2 d2
.
.
.
uM vM dM
p1 t1
p2 t2
.
.
.
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的更多相关文章
- [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)
对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...
- 【BFS】POJ 2251
POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj 2251
http://poj.org/problem?id=2251 一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向. 题意就是从S走到E,#不能走. #include < ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- bzoj 2251: [2010Beijing Wc]外星联络 后缀数组
2251: [2010Beijing Wc]外星联络 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 424 Solved: 232[Submit][ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- ●BZOJ 2251 [2010Beijing Wc]外星联络
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2251 题解: 后缀数组,倍增,RMQ 题意:把重复次数超过 1次的子串按字典序输出它们重复的 ...
随机推荐
- ruby 程序中的文字编码
1,问题 在写一个统计代码行数的脚本时遇到一个问题: 代码: file_name = "code.rb"c = 0File.foreach(file_name) do |x| ne ...
- 浅谈iOS网络编程之一入门
计算机网络,基本上可以抽象是端的通信.实际在通讯中会用到不同的设备,不同的硬件中,为了能友好的传输信息,那么建立一套规范就十分必要了.先来了解一些基本概念 了解网络中传输的都是二进制数据流. 2.了 ...
- C语言 将产生的随机数存入数组,数据不能相同
1.定义一个一维数,数组大小为24. 2.产生0~23的随机数. 3.将产生的随机数存入i数组,要求数组中的每个数据不能相同. 4.补充说明,这个子程序要求每次调用后,这个数组里面就 存放了0~23这 ...
- "奇葩家园“之 asyncTask 与 url 下载篇
asyncTask 是android提供的一个轻量级的异步处理的类,有3个泛型参数,params,progress,result params: 启动任务执行的时候传入的参数比如请求的 url 地址 ...
- iOS 初级数据持久化
数据持久化 什么是数据持久化? 数据的永久存储 为什么要做数据持久化::存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的 数据持久化的本质:数据保存成文件,存储到程序的沙盒中 一.沙 ...
- Python实现SVM(支持向量机)
Python实现SVM(支持向量机) 运行环境 Pyhton3 numpy(科学计算包) matplotlib(画图所需,不画图可不必) 计算过程 st=>start: 开始 e=>end ...
- QT 十六进制整数变为字符串自动补0 && 十进制补零
QString str = QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0')); int a=0001; QStri ...
- 路由设置 windows
打印路由信息: route print 如何临时添加电脑内部路由[ route add 网段 mask 子网掩码 网关] 例如:route add 172.18.0.0 mask 255.255.0. ...
- 思维认知-读mindhacks杂记
1. 导语 无意中浏览知乎,搜索到了mindhacks.cn这个个人geek的网址.mindhacks博主本人是牛人程序员一枚,但他的博客主题涵盖的主要内容确是思维改变生活. 博客链接地址:http: ...
- 微信小程序购物商城系统开发系列
微信小程序购物商城系统开发系列 微信小程序开放公测以来,一夜之间在各种技术社区中就火起来啦.对于它 估计大家都不陌生了,对于它未来的价值就不再赘述,简单一句话:可以把小程序简单理解为一个新的操作系统. ...