uvalive 4973 Ardenia

題意:給出空間兩條線段,求距離。

注意輸出格式!

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<algorithm>
  4 using namespace std;
  5 
  6 struct Point3
  7 {
  8     int x, y, z;
  9     Point3(int x=0, int y=0, int z=0):x(x),y(y),z(z) { }
 10 };
 11 
 12 typedef Point3 Vector3;
 13 
 14 Vector3 operator + (const Vector3& A, const Vector3& B)
 15 {
 16     return Vector3(A.x+B.x, A.y+B.y, A.z+B.z);
 17 }
 18 Vector3 operator - (const Point3& A, const Point3& B)
 19 {
 20     return Vector3(A.x-B.x, A.y-B.y, A.z-B.z);
 21 }
 22 Vector3 operator * (const Vector3& A, int p)
 23 {
 24     return Vector3(A.x*p, A.y*p, A.z*p);
 25 }
 26 
 27 bool operator == (const Point3& a, const Point3& b)
 28 {
 29     return a.x==b.x && a.y==b.y && a.z==b.z;
 30 }
 31 
 32 Point3 read_point3()
 33 {
 34     Point3 p;
 35     scanf("%d%d%d", &p.x, &p.y, &p.z);
 36     return p;
 37 }
 38 
 39 int Dot(const Vector3& A, const Vector3& B)
 40 {
 41     return A.x*B.x + A.y*B.y + A.z*B.z;
 42 }
 43 int Length2(const Vector3& A)
 44 {
 45     return Dot(A, A);
 46 }
 47 Vector3 Cross(const Vector3& A, const Vector3& B)
 48 {
 49     return Vector3(A.y*B.z - A.z*B.y, A.z*B.x - A.x*B.z, A.x*B.y - A.y*B.x);
 50 }
 51 
 52 typedef long long LL;
 53 
 54 LL gcd(LL a, LL b)
 55 {
 56     return b ? gcd(b, a%b) : a;
 57 }
 58 LL lcm(LL a, LL b)
 59 {
 60     return a / gcd(a,b) * b;
 61 }
 62 
 63 struct Rat
 64 {
 65     LL a, b;
 66     Rat(LL a=0):a(a),b(1) { }
 67     Rat(LL x, LL y):a(x),b(y)
 68     {
 69         if(b < 0) a = -a, b = -b;
 70         LL d = gcd(a, b);
 71         if(d < 0) d = -d;
 72         a /= d;
 73         b /= d;
 74     }
 75 };
 76 
 77 Rat operator + (const Rat& A, const Rat& B)
 78 {
 79     LL x = lcm(A.b, B.b);
 80     return Rat(A.a*(x/A.b)+B.a*(x/B.b), x);
 81 }
 82 
 83 Rat operator - (const Rat& A, const Rat& B)
 84 {
 85     return A + Rat(-B.a, B.b);
 86 }
 87 Rat operator * (const Rat& A, const Rat& B)
 88 {
 89     return Rat(A.a*B.a, A.b*B.b);
 90 }
 91 
 92 void updatemin(Rat& A, const Rat& B)
 93 {
 94     if(A.a*B.b > B.a*A.b) A.a = B.a, A.b = B.b;
 95 }
 96 
 97 // 點P到線段AB的距離的平方
 98 Rat Rat_Distance2ToSegment(const Point3& P, const Point3& A, const Point3& B)
 99 {
100     if(A == B) return Length2(P-A);
101     Vector3 v1 = B - A, v2 = P - A, v3 = P - B;
102     if(Dot(v1, v2) < 0) return Length2(v2);
103     else if(Dot(v1, v3) > 0) return Length2(v3);
104     else return Rat(Length2(Cross(v1, v2)), Length2(v1));
105 }
106 
107 // 求異面直線p1+su和p2+tv的公垂線對應的s。如果平行/重合,返回false
108 bool Rat_LineDistance3D(const Point3& p1, const Vector3& u, const Point3& p2, const Vector3& v, Rat& s)
109 {
110     LL b = (LL)Dot(u,u)*Dot(v,v) - (LL)Dot(u,v)*Dot(u,v);
111     if(b == 0) return false;
112     LL a = (LL)Dot(u,v)*Dot(v,p1-p2) - (LL)Dot(v,v)*Dot(u,p1-p2);
113     s = Rat(a, b);
114     return true;
115 }
116 
117 void Rat_GetPointOnLine(const Point3& A, const Point3& B, const Rat& t, Rat& x, Rat& y, Rat& z)
118 {
119     x = Rat(A.x) + Rat(B.x-A.x) * t;
120     y = Rat(A.y) + Rat(B.y-A.y) * t;
121     z = Rat(A.z) + Rat(B.z-A.z) * t;
122 }
123 
124 Rat Rat_Distance2(const Rat& x1, const Rat& y1, const Rat& z1, const Rat& x2, const Rat& y2, const Rat& z2)
125 {
126     return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
127 }
128 
129 int main()
130 {
131     int T;
132     scanf("%d", &T);
133     LL maxx = 0;
134     while(T--)
135     {
136         Point3 A = read_point3();
137         Point3 B = read_point3();
138         Point3 C = read_point3();
139         Point3 D = read_point3();
140         Rat s, t;
141         bool ok = false;
142         Rat ans = Rat(1000000000);
143         if(Rat_LineDistance3D(A, B-A, C, D-C, s))
144             if(s.a > 0 && s.a < s.b && Rat_LineDistance3D(C, D-C, A, B-A, t))
145                 if(t.a > 0 && t.a < t.b)
146                 {
147                     ok = true; // 異面直線/相交直線
148                     Rat x1, y1, z1, x2, y2, z2;
149                     Rat_GetPointOnLine(A, B, s, x1, y1, z1);
150                     Rat_GetPointOnLine(C, D, t, x2, y2, z2);
151                     ans = Rat_Distance2(x1, y1, z1, x2, y2, z2);
152                 }
153         if(!ok)   // 平行直線/重合直線
154         {
155             updatemin(ans, Rat_Distance2ToSegment(A, C, D));
156             updatemin(ans, Rat_Distance2ToSegment(B, C, D));
157             updatemin(ans, Rat_Distance2ToSegment(C, A, B));
158             updatemin(ans, Rat_Distance2ToSegment(D, A, B));
159         }
160         printf("%lld %lld\n", ans.a, ans.b);
161     }
162     return 0;
163 }
View Code

?

轉載于:https://www.cnblogs.com/ITUPC/p/4903196.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397388.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397388.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397388.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

rz和sz上傳下載文件

安裝軟件包 yum install lrzsz 上傳文件&#xff0c;輸入rz選擇文件上傳(可以按住shift鍵多選) # rz sz 下載文件到本地&#xff0c;選擇保存文件夾 # sz dd xshell設置默認上傳下載文件夾 轉載于:https://www.cnblogs.com/fcing/p/9382377.html

上班第一天(6)--一個程序員的成長史(15)

走出公司大門口之后&#xff0c;代是雄看到很多人都朝著一個方向走去。代是雄比較納悶&#xff0c;于是便問保安這是什么情況。“你是新來的吧&#xff1f;連這個都不知道嗎&#xff1f;”保安似乎不屑于回答新人的問題。“我是新來的實習生&#xff0c;”代是雄壓制住了心中的…

自學Java匯報(3)

本周自學Java總結&#xff1a; 繼承語法、成員變量的隱藏和方法的覆蓋、super、final、多態、組合于繼承、初始化順序、部分抽象類。 總用時八小時&#xff0c;編程兩小時。 下周目標&#xff1a;接口、枚舉、異常。轉載于:https://www.cnblogs.com/lianghang/p/9384793.html

怎樣在html中設置首字母大寫,javascript如何設置字符串首字母大寫?

給出一個字符串&#xff0c;如何確保字符串的首字母都大寫&#xff1f;下面本篇文章就來給大家介紹一下使用javascript設置首字母大寫的方法&#xff0c;希望對大家有所幫助。在javascript中&#xff0c;可以使用slice()方法、toUpperCase()方法和toLowerCase()方法來設置首字母…

win2008修改遠程端口

2019獨角獸企業重金招聘Python工程師標準>>> 網絡上找到的一段代碼&#xff0c;保存為.bat&#xff0c;運行修改成功&#xff0c;需要重啟。 echo off color 0a echo ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇ echo ◇◇◇◇修改遠程桌面3389端口批處理◇◇◇◇ ech…

ios7 蘋果原生二維碼掃描(和微信類似)

在ios7蘋果推出了二維碼掃描&#xff0c;以前想要做二維碼掃描&#xff0c;只能通過第三方ZBar與ZXing。 ZBar在掃描的靈敏度上&#xff0c;和內存的使用上相對于ZXing上都是較優的&#xff0c;但是對于 “圓角二維碼” 的掃描確很困難。 ZXing 是 Google Code上的一個開源的條…

有符號位和無符號位。——int8疑問有感

學習go語言的數據類型&#xff0c;看見int、int8、int16很是疑惑&#xff0c;int8是什么意思&#xff1f;查詢資料進行綜合解釋大概如下&#xff1a; Int8是有符號位8位整形&#xff08;-128到127&#xff09;&#xff0c;隨即產生疑惑&#xff0c;為什么負數可表示到-128&…

html幫助文檔亂碼,使用doxygen生成的幫助文檔,中文出現亂碼的問題

今天使用doxygen工具生成幫助文檔發現中文注釋都是亂碼。然后根據網上的要求把Exper>>Input>>INPUT_ENCODING&#xff1a;(輸入文件的編碼) UTF-8 改成 GBK 或者 GB2312Exper>>HTML>>CHM_INDEX_ENCODING&#xff1a;(輸出文件的編碼) UTF-8 改成 GBK 或…

Java并發編程--理解ThreadLocal

另一篇博文&#xff1a;Hibernet中的ThreadLocal使用 http://www.cnblogs.com/gnivor/p/4440776.html 本文參考&#xff1a;http://blog.csdn.net/lufeng20/article/details/24314381http://www.cnblogs.com/chenying99/articles/3405161.html ThreadLocal類接口很簡單&#xf…

delphi Post數據到網頁

varhttp: TIdHttp;sendtoserver: TStringStream;str: string; beginhttp : TIdHttp.Create(); // 創建http.HandleRedirects : True; // 允許轉頭http.ReadTimeout : 3000; …

python之路——迭代器與生成器

要了解for循環是怎么回事兒&#xff0c;咱們還是要從代碼的角度出發。 首先&#xff0c;我們對一個列表進行for循環。 for i in [1,2,3,4]: print(i) 上面這段代碼肯定是沒有問題的&#xff0c;但是我們換一種情況&#xff0c;來循環一個數字1234試試 for i in 1234print(i) 結…

HTML頁面顯示透視效果,html – CSS – 對背景圖像的“敲除”/透視效果

我認為這里的想法是圖像必須足夠大,以覆蓋網頁或至少父母div ..然后,您可以將圖像應用于容器和’inner’div的背景.覆蓋可以通過偽元素而不是單獨的div來實現.修訂結構 –.bck {position: relative;height: 800px;width: 100%;background:url(http://webneel.com/wallpaper/sit…

DFS分布式文件系統--管理篇

DFS分布式文件系統--管理篇參考文檔&#xff1a;淺談DFS分布式文件系統DFS 命名空間 和 DFS 復制概述續DFS分布式文件系統--基礎篇DFS分布式文件系統--部署篇添加命名空間服務器&#xff08;添加第二臺命名空間服務器 NameSrv02)成功后如下圖&#xff1a;“從顯示區域隱藏命名空…

Linux 0-1 修改主機名及IP地址

1.修改主機名 hostname 查看主機名 vi /etc/sysconfig/network 修改hostname主機名 vi /etc/hosts 修改127.0.1 主機名 service network restart #/etc/hosts 在域名解析時優先于DNS服務器2.IP地址 ifconfig 查看目前網絡卡信息 cd /etc/sysconfig/network-scripts ls查看…

html漸變顏色代碼表,漸變顏色代碼表

漸變顏色代碼表2020-12-24素材&#xff1a;網絡 編輯&#xff1a;唔爾灬#000000#2F0000#600030#460046#28004D#272727#4D0000#820041#5E005E#3A006F#3C3C3C#600000#9F0050#750075#4B0091#4F4F4F#750000#BF0060#930093#5B00AE#5B5B5B#930000#D9006C#AE00AE#6F00D2#6C6C6C#AE0000…

js貪心算法---背包問題

/** param {Object} capacity 背包容量 6* param {Object} weights 物品重量 [2,3,4]* param {Object} values 物品價值 [3,4,5]*///貪心算法&#xff0c;只能算&#xff0c;可以分割的物品&#xff0c;如果不能分割物品&#xff0c;只能得到近似解&#xff0c;不分割物品&…

Spring利用JDBCTemplate實現批量插入和返回id

1、先介紹一下java.sql.Connection接口提供的三個在執行插入語句后可取的自動生成的主鍵的方法&#xff1a; //第一個是 PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException; 其中autoGenerateKeys 有兩個可選值&#xff1a;Stat…

jsp壓縮html,使用HtmlCompressor壓縮JSP編譯的Html代碼

HtmlCompressor 能夠刪除多余的HTML代碼。它提供多種方法&#xff1a;刪除無用的空行、刪除注釋以及刪除無用的表格等等&#xff0c;簡單而有效。在Java代碼中可以這樣使用&#xff1a;String html getHtml(); //需要處理的Html代碼HtmlCompressor compressor new HtmlCompre…

LVS負載均衡(3)——LVS工作模式與工作原理

LVS介紹及工作原理1. LVS 介紹LVS,Linux Virtual Server 的簡寫&#xff0c;意即 Linux 虛擬服務器&#xff0c;是一個虛擬的服務器集群系統&#xff0c;可以在 UNIX/Linux 平臺下實現負載均衡集群功能。文章&#xff1a;LVS項目介紹LVS集群體系結構LVS集群的IP負載均衡技術LVS…

保留凸性的方式:一個凸函數在一個隨機變量上的期望仍然是凸函數

設函數 gg 是實數范圍內的一個凸函數&#xff0c;DD 是一個隨機變量&#xff0c; 那么函數 GEDg(y?D)GEDg(y?D) 仍然是一個凸函數。 證明&#xff1a;記 θθθθ, yy 與 yy 是任意兩個數 ≥θG(y)θG(y)θEDg(y?D)θEDg(y?D)ED[θg(y?D)θ(gy?D)]ED[g(θyθy?D)]G(θyθ…