AIZU 2251
Merry Christmas
Time Limit : 8 sec, Memory Limit : 65536 KBProblem 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 ,每个询问都当作一个点,做最小路径覆盖即可
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 7 using namespace std; 8 9 #define maxn 105 10 #define INF (1 << 30) 11 12 int n,m,l; 13 int dis[maxn][maxn],p[1005],t[1005]; 14 int match[1005]; 15 bool vis[1005]; 16 vector<int> G[1005]; 17 18 void floyd() { 19 for(int k = 0; k < n; k++) { 20 for(int i = 0; i < n; ++i) { 21 for(int j = 0; j < n; ++j) { 22 if(dis[i][k] != INF && dis[k][j] != INF) { 23 dis[i][j] = min(dis[i][j], 24 dis[i][k] + dis[k][j]); 25 } 26 27 } 28 } 29 } 30 } 31 32 bool dfs(int u) { 33 for(int i = 0; i < G[u].size(); ++i) { 34 int v = G[u][i]; 35 if(vis[v]) continue; 36 vis[v] = 1; 37 if(match[v] == -1 || dfs(match[v])) { 38 match[v] = u; 39 return true; 40 } 41 42 } 43 44 return false; 45 } 46 47 void build() { 48 for(int i = 1; i <= l; ++i) { 49 for(int j = 1; j <= l; ++j) { 50 if(i == j) continue; 51 if(dis[ p[i] ][ p[j] ] != INF && 52 dis[ p[i] ][ p[j] ] + t[i] <= t[j]) { 53 54 G[i].push_back(j); 55 } 56 } 57 } 58 } 59 60 void solve() { 61 floyd(); 62 63 build(); 64 65 int ans = 0; 66 67 for(int i = 1; i <= l; ++i) match[i] = -1; 68 69 for(int i = 1; i <= l; ++i) { 70 memset(vis,0,sizeof(vis)); 71 if(dfs(i)) ++ans; 72 } 73 74 // printf("ans = %d\n",ans); 75 76 printf("%d\n",l - ans); 77 78 79 80 81 } 82 83 int main() 84 { 85 //freopen("sw.in","r",stdin); 86 87 while(~scanf("%d%d%d",&n,&m,&l)) { 88 89 if(!n && !m && !l) break; 90 91 for(int i = 0; i < n; ++i) { 92 for(int j = 0; j < n; ++j) { 93 dis[i][j] = i == j ? 0 : INF; 94 } 95 } 96 97 for(int i = 1; i <= l; ++i) G[i].clear(); 98 99 for(int i = 1; i <= m; ++i) { 100 int u,v,w; 101 scanf("%d%d%d",&u,&v,&w); 102 dis[u][v] = dis[v][u] = w; 103 } 104 105 for(int i = 1; i <= l; ++i) { 106 scanf("%d%d",&p[i],&t[i]); 107 } 108 109 solve(); 110 111 } 112 113 return 0; 114 }