USACO 6.4 Electric Fences

Electric Fences
Kolstad & Schrijvers

Farmer John has decided to construct electric fences. He has fenced his fields into a number of bizarre shapes and now must find the optimal place to locate the electrical supply to each of the fences.

A single wire must run from some point on each and every fence to the source of electricity. Wires can run through other fences or across other wires. Wires can run at any angle. Wires can run from any point on a fence (i.e., the ends or anywhere in between) to the electrical supply.

Given the locations of all F (1 <= F <= 150) fences (fences are always parallel to a grid axis and run from one integer gridpoint to another, 0 <= X,Y <= 100), your program must calculate both the total length of wire required to connect every fence to the central source of electricity and also the optimal location for the electrical source.

The optimal location for the electrical source might be anywhere in Farmer John's field, not necessarily on a grid point.

PROGRAM NAME: fence3

INPUT FORMAT

The first line contains F, the number of fences.
F subsequent lines each contain two X,Y pairs each of which denotes the endpoints of a fence.

SAMPLE INPUT (file fence3.in)

3
0 0 0 1
2 0 2 1
0 3 2 3

OUTPUT FORMAT

On a single line, print three space-separated floating point numbers, each with a single decimal place. Presume that your computer's output library will round the number correctly.

The three numbers are:

  • the X value of the optimal location for the electricity,
  • the Y value for the optimal location for the electricity, and
  • the total (minimum) length of the wire required.

SAMPLE OUTPUT (file fence3.out)

1.0 1.6 3.7

——————————————————————————————————————————题解
题解用了一个不是很像模拟退火的方法,因为模拟退火需要以一个随机的概率接受不优的解
用先跳20步,尝试跳10次,再缩减10倍,往复5次
然后可以得到一个最优解,这个办法挺随机的
 1 /*
 2 LANG: C++
 3 PROG: fence3
 4 */
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cstring>
 9 #include <cmath>
10 #define siji(i,x,y) for(int i=(x); i<=(y) ; ++i)
11 #define ivorysi
12 #define o(x) ((x)*(x))
13 using namespace std;
14 typedef long long ll;
15 int f;
16 struct data{
17     int xs,ys,xt,yt;
18 }seg[155];
19 void init() {
20     scanf("%d",&f);
21     siji(i,1,f) {
22         scanf("%d%d%d%d",&seg[i].xs,&seg[i].ys,&seg[i].xt,&seg[i].yt);
23         if(seg[i].xt<seg[i].xs) swap(seg[i].xt,seg[i].xs);
24         if(seg[i].yt<seg[i].ys) swap(seg[i].yt,seg[i].ys);
25     }
26 }
27 double dist(double x,double y) {
28     double res=0.0;
29     siji(i,1,f) {
30         double xid=min(fabs(seg[i].xt-x),fabs(seg[i].xs-x));
31         if(x>=seg[i].xs && x<=seg[i].xt) xid=0;
32         double yid=min(fabs(seg[i].yt-y),fabs(seg[i].ys-y));
33         if(y>=seg[i].ys && y<=seg[i].yt) yid=0;
34         res+=sqrt(o(xid)+o(yid));
35     }
36     return res;
37 }
38 void solve() {
39     init();
40     //if(n==7) {cout<<"12198297600"<<endl;return;}
41     double elecx=0.0,elecy=0.0;
42     double direx[4]={1.0,0.0,-1.0,0.0},direy[4]={0.0,1.0,0.0,-1.0};
43     double T=20.0;
44     double bestnum=dist(0.0,0.0);
45     siji(b,1,50) {
46         if(b%10==0) T*=0.1;
47         int best=-1;
48         siji(i,0,3) {
49             elecx+=direx[i]*T;
50             elecy+=direy[i]*T;
51             double temp=dist(elecx,elecy);
52             if(temp<bestnum) 
53                 bestnum=temp,best=i;
54             elecx-=direx[i]*T;
55             elecy-=direy[i]*T;
56         }
57         if(best!=-1) {
58             elecx+=direx[best]*T;
59             elecy+=direy[best]*T;
60         }
61         bestnum=dist(elecx,elecy);
62     }
63     printf("%.1lf %.1lf %.1lf\n",elecx,elecy,bestnum);
64 }
65 int main(int argc, char const *argv[])
66 {
67 #ifdef ivorysi
68     freopen("fence3.in","r",stdin);
69     freopen("fence3.out","w",stdout);
70 #else
71     freopen("f1.in","r",stdin);
72     //freopen("f1.out","w",stdout);
73 #endif
74     solve();
75     return 0;
76 }

 

 

转载于:https://www.cnblogs.com/ivorysi/p/6939552.html

内容概要:本文详细比较了GPU、TPU专用AI芯片在大模型推理优化方面的性能、成本及适用场景。GPU以其强大的并行计算能力和高带宽显存,适用于多种类型的神经网络模型和计算任务,尤其适合快速原型开发和边缘计算设备。TPU专为机器学习设计,擅长处理大规模矩阵运算密集型任务,如Transformer模型的推理,具有高吞吐量和低延迟特性,适用于自然语言处理和大规模数据中心的推理任务。专用AI芯片通过高度定制化架构,针对特定神经网络模型进行优化,如卷积神经网络(CNN),在处理特定任务时表现出色,同时具备低功耗和高能效比的优势,适用于边缘计算设备。文章还介绍了各自的优化工具和框架,如CUDA、TensorRT、TPU编译器等,并从硬件成本、运营成本和开发成本三个角度进行了成本对比。 适合人群:从事人工智能、深度学习领域的研究人员和技术人员,尤其是对大模型推理优化感兴趣的读者。 使用场景及目标:①帮助读者理解GPU、TPU和专用AI芯片在大模型推理中的优缺点;②为选择适合的硬件平台提供参考依据,以实现最优的推理性能和成本效益;③介绍各种优化工具和框架,帮助开发者高效部署和优化模型。 其他说明:本文不仅涵盖了硬件架构特性,还深入探讨了优化技术和应用场景,旨在为读者提供全面的技术参考。在选择硬件平台时,需综合考虑具体任务需求、预算限制及开发资源等因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值