比赛链接:https://www.bnuoj.com/v3/contest_show.php?cid=8520
A.无非两种情况,点在体里和点在体外。在体外分三种情况,分别是到顶点的距离最小、到棱的距离最小、到面的距离最小。坑在long long于是悲催了一发。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 LL x[5], y[5], z[5]; 6 7 LL dis(LL x1, LL y1, LL z1, LL x2, LL y2, LL z2) { 8 LL a = x1 - x2, b = y1 - y2, c = z1 - z2; 9 return a * a + b * b + c * c; 10 } 11 12 LL d(LL x1, LL y1, LL x2, LL y2) { 13 LL a = x1 - x2, b = y1 - y2; 14 return a * a + b * b; 15 } 16 17 int main() { 18 //freopen("in", "r", stdin); 19 while(~scanf("%lld%lld%lld",&x[0],&y[0],&z[0])) { 20 for(LL i = 1; i <= 2; i++) scanf("%lld%lld%lld",&x[i],&y[i],&z[i]); 21 if(x[1]<=x[0]&&x[0]<=x[2]&&y[1]<=y[0]&&y[0]<=y[2]&&z[1]<=z[0]&&z[0]<=z[2]) { puts("0"); continue; } 22 LL ret = 100000000000LL; 23 for(LL i = 1; i <= 2; i++) { 24 for(LL j = 1; j <= 2; j++) { 25 for(LL k = 1; k <= 2; k++) { 26 ret = min(ret, dis(x[0],y[0],z[0],x[i],y[j],z[k])); 27 } 28 } 29 } 30 if(x[1]<=x[0]&&x[0]<=x[2]&&!(y[1]<=y[0]&&y[0]<=y[2]&&z[1]<=z[0]&&z[0]<=z[2])) { 31 ret = min(ret, d(y[0],z[0],y[1],z[1])); 32 ret = min(ret, d(y[0],z[0],y[2],z[2])); 33 ret = min(ret, d(y[0],z[0],y[1],z[2])); 34 ret = min(ret, d(y[0],z[0],y[2],z[1])); 35 } 36 if(y[1]<=y[0]&&y[0]<=y[2]&&!(x[1]<=x[0]&&x[0]<=x[2]&&z[1]<=z[0]&&z[0]<=z[2])) { 37 ret = min(ret, d(x[0],z[0],x[1],z[1])); 38 ret = min(ret, d(x[0],z[0],x[2],z[2])); 39 ret = min(ret, d(x[0],z[0],x[1],z[2])); 40 ret = min(ret, d(x[0],z[0],x[2],z[1])); 41 } 42 if(z[1]<=z[0]&&z[0]<=z[2]&&!(y[1]<=y[0]&&y[0]<=y[2]&&x[1]<=x[0]&&x[0]<=x[2])) { 43 ret = min(ret, d(x[0],y[0],x[1],y[1])); 44 ret = min(ret, d(x[0],y[0],x[2],y[2])); 45 ret = min(ret, d(x[0],y[0],x[2],y[1])); 46 ret = min(ret, d(x[0],y[0],x[1],y[2])); 47 } 48 49 if(y[1]<=y[0]&&y[0]<=y[2]&&z[1]<=z[0]&&z[0]<=z[2]&&!(x[1]<=x[0]&&x[0]<=x[2])) { 50 ret = min(ret, (x[0]-x[1])*(x[0]-x[1])); 51 ret = min(ret, (x[0]-x[2])*(x[0]-x[2])); 52 } 53 if(x[1]<=x[0]&&x[0]<=x[2]&&z[1]<=z[0]&&z[0]<=z[2]&&!(y[1]<=y[0]&&y[0]<=y[2])) { 54 ret = min(ret, (y[0]-y[1])*(y[0]-y[1])); 55 ret = min(ret, (y[0]-y[2])*(y[0]-y[2])); 56 } 57 if(y[1]<=y[0]&&y[0]<=y[2]&&x[1]<=x[0]&&x[0]<=x[2]&&!(z[1]<=z[0]&&z[0]<=z[2])) { 58 ret = min(ret, (z[0]-z[1])*(z[0]-z[1])); 59 ret = min(ret, (z[0]-z[2])*(z[0]-z[2])); 60 } 61 printf("%lld ", ret); 62 } 63 return 0; 64 }
E.分类讨论,容易漏情况。
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 LL a, b, c, ret; 5 6 int main() { 7 // freopen("in", "r", stdin); 8 while(~scanf("%lld%lld%lld",&a,&b,&c)) { 9 if(a >= 2) ret = a + 2 * b + 3 * c; 10 else if(a == 1) { 11 if(b) ret = a + 2 * b + 3 * c; 12 else ret = 2 * c + 1; 13 } 14 else { 15 if(b == 1) ret = 2 * c + 1; 16 else if(b > 1) { 17 if(c) ret = 2 * b + 3 * c - 2; 18 else ret = b; 19 } 20 else ret = c; 21 } 22 printf("%lld ",ret); 23 } 24 }
F.写挂了,待补。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef pair<int,int> pii; 5 const int maxn = 100100; 6 const int inf = 90000000; 7 int n, m, q; 8 int u, v; 9 map<pii,bool> vis; 10 map<pii,int> dis; 11 vector<int> G[maxn]; 12 13 void bfs(int s, int t) { 14 queue<int> q; 15 int u, v; 16 q.push(s); dis[pii(s,s)] = 0; vis[pii(s,s)] = 1; 17 while(!q.empty()) { 18 u = q.front(); q.pop(); 19 if(u == t) return; 20 for(int i = 0; i < G[u].size(); i++) { 21 v = G[u][i]; 22 dis[pii(u,v)] = dis[pii(v,u)] = 1; 23 vis[pii(u,v)] = vis[pii(v,u)] = 1; 24 if(dis.find(pii(s,t)) == dis.end()) { 25 26 dis[pii(s,v)] = dis[pii(s,u)] + 1; 27 dis[pii(v,s)] = dis[pii(u,s)] + 1; 28 vis[pii(s,v)] = vis[pii(v,s)] = 1; 29 q.push(v); 30 } 31 } 32 } 33 } 34 35 int main() { 36 freopen("in", "r", stdin); 37 while(~scanf("%d%d%d",&n,&m,&q)) { 38 vis.clear(); dis.clear(); 39 for(int i = 1; i <= n; i++) G[i].clear(); 40 for(int i = 0; i < m; i++) { 41 scanf("%d%d",&u,&v); 42 if(vis.find(pii(u,v)) == vis.end()) { 43 vis[pii(u,v)] = 1; 44 vis[pii(v,u)] = 1; 45 G[u].push_back(v); 46 G[v].push_back(u); 47 } 48 } 49 vis.clear(); 50 int x, y; 51 for(int i = 0; i < q; i++) { 52 scanf("%d%d",&x,&y); 53 if(x == y) puts("0"); 54 else { 55 if(vis.find(pii(x,y)) != vis.end()) { 56 printf("%d ", dis[pii(x,y)]); 57 } 58 else { 59 bfs(x, y); 60 printf("%d ", dis[pii(x,y)]); 61 } 62 } 63 } 64 } 65 return 0; 66 }