标签:nal names text putc com 拓扑序 1.4 head logs
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5647 Accepted Submission(s): 2036
1 //2017-08-27 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <iomanip> 8 #include <cmath> 9 10 using namespace std; 11 12 const int N = 5010; 13 const int M = N*N; 14 const double EPS = 1e-6; 15 int head[N], rhead[N], tot, rtot; 16 struct Edge{ 17 int to, next; 18 }edge[M], redge[M]; 19 20 void init(){ 21 tot = 0; 22 rtot = 0; 23 memset(head, -1, sizeof(head)); 24 memset(rhead, -1, sizeof(rhead)); 25 } 26 27 void add_edge(int u, int v){ 28 edge[tot].to = v; 29 edge[tot].next = head[u]; 30 head[u] = tot++; 31 32 redge[rtot].to = u; 33 redge[rtot].next = rhead[v]; 34 rhead[v] = rtot++; 35 } 36 37 vector<int> vs;//后序遍历顺序的顶点列表 38 bool vis[N]; 39 int cmp[N];//所属强连通分量的拓扑序 40 41 //input: u 顶点 42 //output: vs 后序遍历顺序的顶点列表 43 void dfs(int u){ 44 vis[u] = true; 45 for(int i = head[u]; i != -1; i = edge[i].next){ 46 int v = edge[i].to; 47 if(!vis[v]) 48 dfs(v); 49 } 50 vs.push_back(u); 51 } 52 53 //input: u 顶点编号; k 拓扑序号 54 //output: cmp[] 强连通分量拓扑序 55 void rdfs(int u, int k){ 56 vis[u] = true; 57 cmp[u] = k; 58 for(int i = rhead[u]; i != -1; i = redge[i].next){ 59 int v = redge[i].to; 60 if(!vis[v]) 61 rdfs(v, k); 62 } 63 } 64 65 //Strongly Connected Component 强连通分量 66 //input: n 顶点个数 67 //output: k 强连通分量数; 68 int scc(int n){ 69 memset(vis, 0, sizeof(vis)); 70 vs.clear(); 71 for(int u = 0; u < n; u++) 72 if(!vis[u]) 73 dfs(u); 74 int k = 0; 75 memset(vis, 0, sizeof(vis)); 76 for(int i = vs.size()-1; i >= 0; i--) 77 if(!vis[vs[i]]) 78 rdfs(vs[i], k++); 79 return k; 80 } 81 82 int n; 83 struct Point{ 84 int x, y; 85 }point[N]; 86 87 //input: 两个点 88 //output: 两点间距离 89 double distance(Point a, Point b){ 90 return sqrt((double)(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); 91 } 92 93 //input:radius 半径 94 //output:true 通过选取某些点可以得到radius的分数,false 无法得到radius的分数 95 bool check(double radius){ 96 init(); 97 for(int i = 0; i < 2*n; i++){ 98 for(int j = i+1; j < 2*n; j++){ 99 if((i^1) == j)continue; 100 if(distance(point[i], point[j]) < 2*radius){//i与j存在矛盾 101 add_edge(i^1, j);// NOT i -> j 102 add_edge(j^1, i);// NOT j -> i 103 } 104 } 105 } 106 scc(2*n); 107 for(int i = 0; i < 2*n; i += 2){ 108 if(cmp[i] == cmp[i^1]) 109 return false; 110 } 111 return true; 112 } 113 114 int main() 115 { 116 std::ios::sync_with_stdio(false); 117 //freopen("inputC.txt", "r", stdin); 118 while(cin>>n){ 119 for(int i = 0; i < n; i++){ 120 cin>>point[2*i].x>>point[2*i].y>>point[2*i+1].x>>point[2*i+1].y; 121 } 122 double l = 0.0, r = 40000.0, mid, ans = 0; 123 while(r-l > EPS){ 124 mid = (l+r)/2; 125 if(check(mid)){ 126 ans = mid; 127 l = mid; 128 }else 129 r = mid; 130 } 131 cout.setf(ios::fixed); 132 cout<<setprecision(2)<<ans<<endl; 133 } 134 135 return 0; 136 }
标签:nal names text putc com 拓扑序 1.4 head logs
原文地址:http://www.cnblogs.com/Penn000/p/7440437.html