SRM 622 div1 250
2014-05-30 23:13
381 查看
Problem Statement
In the Republic of Nlogonia there are N cities. For convenience, the cities are numbered 0 through N-1. For each two different cities i and j, there is a direct one-way road from i to j. You are given the lengths of those roads as a vector <string> dist with N elements, each with N characters. For each i and j, the character dist[i][j] represents the length of the road from i to j. The lengths of roads are integers between 1 and 9, inclusive, and they are represented by digits '1' through '9'. That is, for distinct i and j, dist[i][j] will be between '1' and '9'. For each i, dist[i][i] will be '0'. Note that the roads from i to j and from j to i may have different lengths. Every year on Algorithms Day (the most important holiday in Nlogonia) people travel between the cities. More precisely, for each pair of distinct cities i and j, one full bus of people travels from i to j. Each of those buses drives along a shortest path from its origin to its destination. If there are multiple shortest paths, the bus driver picks one of them arbitrarily. The roads in Nlogonia are currently limited. You are given an int T with the following meaning: each of the current roads is only safe if it is guaranteed that there will be strictly fewer than T buses driving along the road. In other words, a road is unsafe if it is possible that T or more buses will use it. The government wants to rebuild all unsafe roads before the next Algorithms Day. Return the sum of lengths of all unsafe roads.
Definition
Class:
BuildingRoutes
Method:
build
Parameters:
vector <string>, int
Returns:
int
Method signature:
int build(vector <string> dist, int T)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Constraints
-
N will be between 2 and 50, inclusive.
-
dist will contain exactly N elements.
-
Each element of dist will contain exactly N characters.
-
For each i, dist[i][i] will be '0'.
-
For each pair of distinct i and j, dist[i][j] will be between '1' and '9', inclusive.
-
T will be between 1 and 2500, inclusive.
Examples
0)
{"011",
"101",
"110"}
1
Returns: 6
As T=1, a road is unsafe as soon as it is possible that a bus will use it. Each of the six roads in this test case belongs to some shortest path, hence each of them is unsafe
1)
{"033",
"309",
"390"}
1
Returns: 12
The roads 1->2 and 2->1 (the two roads of length 9) will not be used by any bus. Only the four remaining roads are unsafe in this case.
2)
{"0123",
"1023",
"1203",
"1230"}
2
Returns: 5
3)
{"05789654",
"10347583",
"65085479",
"55602398",
"76590934",
"57939045",
"12345608",
"68647640"}
3
Returns: 40
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
View Code
In the Republic of Nlogonia there are N cities. For convenience, the cities are numbered 0 through N-1. For each two different cities i and j, there is a direct one-way road from i to j. You are given the lengths of those roads as a vector <string> dist with N elements, each with N characters. For each i and j, the character dist[i][j] represents the length of the road from i to j. The lengths of roads are integers between 1 and 9, inclusive, and they are represented by digits '1' through '9'. That is, for distinct i and j, dist[i][j] will be between '1' and '9'. For each i, dist[i][i] will be '0'. Note that the roads from i to j and from j to i may have different lengths. Every year on Algorithms Day (the most important holiday in Nlogonia) people travel between the cities. More precisely, for each pair of distinct cities i and j, one full bus of people travels from i to j. Each of those buses drives along a shortest path from its origin to its destination. If there are multiple shortest paths, the bus driver picks one of them arbitrarily. The roads in Nlogonia are currently limited. You are given an int T with the following meaning: each of the current roads is only safe if it is guaranteed that there will be strictly fewer than T buses driving along the road. In other words, a road is unsafe if it is possible that T or more buses will use it. The government wants to rebuild all unsafe roads before the next Algorithms Day. Return the sum of lengths of all unsafe roads.
Definition
Class:
BuildingRoutes
Method:
build
Parameters:
vector <string>, int
Returns:
int
Method signature:
int build(vector <string> dist, int T)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Constraints
-
N will be between 2 and 50, inclusive.
-
dist will contain exactly N elements.
-
Each element of dist will contain exactly N characters.
-
For each i, dist[i][i] will be '0'.
-
For each pair of distinct i and j, dist[i][j] will be between '1' and '9', inclusive.
-
T will be between 1 and 2500, inclusive.
Examples
0)
{"011",
"101",
"110"}
1
Returns: 6
As T=1, a road is unsafe as soon as it is possible that a bus will use it. Each of the six roads in this test case belongs to some shortest path, hence each of them is unsafe
1)
{"033",
"309",
"390"}
1
Returns: 12
The roads 1->2 and 2->1 (the two roads of length 9) will not be used by any bus. Only the four remaining roads are unsafe in this case.
2)
{"0123",
"1023",
"1203",
"1230"}
2
Returns: 5
3)
{"05789654",
"10347583",
"65085479",
"55602398",
"76590934",
"57939045",
"12345608",
"68647640"}
3
Returns: 40
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
#include "iostream" #include "cstdio" #include "cstring" #include "cstdlib" #include "cmath" #include "algorithm" #include "vector" #include "list" #include "map" #include "queue" #include "set" #include "stack" using namespace std; #define mset(a) memset(a,0,sizeof(a)) class BuildingRoutes{ public: int g[51][51], d[51][51], cnt[51][51], n, ans; void floyd(){ for(int k = 0; k < n; k++){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ d[i][j] = min(d[i][k]+d[k][j], d[i][j]); } } } } void run(int t){ mset(cnt); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ for(int x = 0; x < n; x++){ for(int y = 0; y < n; y++){ if(d[x][y] == d[x][i]+g[i][j]+d[j][y]) cnt[i][j]++; } } } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(cnt[i][j] >= t) ans += g[i][j]; } } } int build(vector <string> dist, int T){ n = dist[0].size(); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ g[i][j] = dist[i][j]-'0'; d[i][j] = g[i][j]; } } floyd(); ans = 0; run(T); return ans; } };
View Code
相关文章推荐
- SRM 622 div1
- TopCoder算法竞赛题3:SRM 147 DIV 2, 250-point
- TopCoder 250 points 8-SRM 147 DIV 2 176.36/250 70.54%
- SRM 575 250 DIV2
- TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%
- topcoder SRM 543 div2 250
- TopCoder算法竞赛题4:SRM 148 DIV 2, 250-point
- topcoder SRM 637 div2+div1 250
- TopCoder 250 points 10-SRM 148 DIV 2 167.20/250 66.88%
- TopCoder 250 points 14-SRM 150 DIV 2 86.25/250 34.50%
- TopCoder SRM 596 DIV 1 250
- Topcoder SRM 652 DIV1 250
- topcoder SRM 541 div2 250
- TC SRM 655 DIV1 250,500pt
- SRM 594 DIV1 250
- 小朋友学TopCoder(7):SRM726 DIV2 250-point
- SRM 597 DIV2 250
- 状态压缩DP SRM 667 Div1 250
- SRM 397 DIV 2 [250]
- TopCoder 250 points 15-SRM 151 DIV 1 121.73/250 48.69%