CodeVS 1344 线型网络
Sol
随机化算法+哈密顿路径.
好厉害的题...首先都会想到状压DP对吧,复杂度 \(O(n^2 2^n)\) .
\(n=20\)
exm?? \(10^8\)
有一种算法就是随机化算法 再调整.
通过随机化算法,再 \(O(n^2)\) 来调整.
调整方式如下:
如果有 \(dis(i-1,i)+dis(j,j+1)>dis(i-1,j)+dis(i,j+1)\)
那么就将区间 \([i,j]\) 翻转...
非常神奇吧 关于证明原文中并没有,总之这样会导致很多不同的方案收束到同一方案,造成方案数的减少,来以随机概率获得正确结果.
PS:简直就是骗分神奇的方法...
原文链接:http://www.doc88.com/p-772451936672.html
Code
#include<cstdio>
#include<cmath>
#include<ctime>
#include<utility>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std; const int N = 25;
#define mpr(a,b) make_pair(a,b)
#define sqr(x) ((x)*(x)) int n,T;
pair<int,int> p[N];
int a[N];double d[N][N];
double ans=9999999999.0; inline int in(int x=0,char ch=getchar(),int v=1){
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*v; }
double dis(int u,int v){ return sqrt((double)sqr(p[u].first-p[v].first)+sqr(p[u].second-p[v].second)); }
int main(){
srand(time(0));
n=in();
for(int i=1;i<=n;i++) p[i]=mpr(in(),in()),a[i]=i;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) d[i][j]=dis(i,j); // for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) printf("%.2lf%c",d[i][j]," \n"[j==n]);
// for(int i=1;i<=n;i++) cout<<p[i].first<<" "<<p[i].second<<endl; for(T=2000;T--;){
random_shuffle(a+1,a+n+1);
double tmp=0; // cout<<"***********"<<endl;
// for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl; for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++)
if(d[a[i-1]][a[i]]+d[a[j]][a[j+1]]>d[a[i-1]][a[j]]+d[a[i]][a[j+1]]) reverse(a+i,a+j+1);
for(int i=1;i<n;i++) tmp+=d[a[i]][a[i+1]]; // cout<<"***********"<<endl;
// for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
// cout<<"tmp="<<tmp<<endl; ans=min(ans,tmp);
}printf("%.2lf\n",ans);
return 0;
}
CodeVS 1344 线型网络的更多相关文章
- codevs 1344 模拟退火
1344 线型网络 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamo 题目描述 Description 有 N ( <=20 ) 台 PC 放在机房内 ...
- NS3网络仿真(6): 总线型网络
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载.但请保留作者信息 在NS3提供的第一个演示样例first.py中,模拟了一个点对点的网络,接下来的一个演示样例代码模 ...
- Cogs 6. 线型网络
6. 线型网络 ★★☆ 输入文件:linec.in 输出文件:linec.out 简单对比时间限制:1 s 内存限制:256 MB [问题描述] 有 N(N<=20)台 PC 放 ...
- Codevs 3578 无线网络发射器选址== NOIP 2014 Day2 T1
3578 无线网络发射器选址 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 白银 Silver 题目描述 Description 随着智能手机的日益普及,人们对无线网的需求日益增大. ...
- cogs 线型网络(状压dp)
/* 需要好大的空间..... 而且lowbit理解的不是很好 先放到博客里 以后慢慢研究 */ #include<iostream> #include<cstdio> #in ...
- CodeVS1344 线型网络
题目描述 Description 有 N ( <=20 ) 台 PC 放在机房内,现在要求由你选定一台 PC,用共 N-1 条网线从这台机器开始一台接一台地依次连接他们,最后接到哪个以及连接的顺 ...
- [Cogs] 线型网络
题面 http://cogs.pro:8080/cogs/problem/problem.php?pid=6 题解 https://www.zybuluo.com/wsndy-xx/note/1135 ...
- 前端学HTTP之网络基础
× 目录 [1]网络 [2]OSI [3]TCP/IP 前面的话 HTTP协议对于前端工程师是非常重要的.我们在浏览网站时,访问的每一个WEB页面都需要使用HTTP协议实现.如果不了解HTTP协议,就 ...
- Java基础之网络编程
网络编程:1.网络编程概述 (1)网络模型 OSI参考模型 TCP/IP参考模型 (2)网络通讯要素 IP地址 端口号 传输协议 (3)网络通讯前提: **找到对方IP **数据要发送到指定端口.为了 ...
随机推荐
- C#----使用WindowsMediaPlayer 同时播放多个声音
使用Windows Media Player 其实就是使用组件AxWindowsMediaPlayer. 添加两个引用:Interop.WMPLib.dll和AxInterop.WMPLib.dll. ...
- B1/B2签证拒签
http://www.mcdvisa.com/html/News/USA_visa_news/201529/152917GE.html
- jquermobile 安装
代码 <script src="../Public/js/jquery-1.11.1.min.js"></script> <script src=&q ...
- Array subscript is not an integer
字典的字母写成大写了,也不能查成出来,没有报没有这个字典,反而报这个错……找了好久
- Winform添加Label
Info from : http://www.csharpwin.com/csharpspace/6253r7952.shtml 本例子主要是介绍如何在 C#开发WinForm中加入一个组件,如果你想 ...
- 捕获EF提交异常
try { } catch (DbEntityValidationException dbex) { string errMsg = string.Empty; foreach (var eve in ...
- Form表单中的action路径问题,form表单action路径《jsp--->Servlet路劲问题》这个和上一个《jsp--->Servlet》文章有关
Form表单中的action路径问题,form表单action路径 热度5 评论 50 www.BkJia.Com 网友分享于: 2014-08-14 08:08:01 浏览数44525次 ...
- thinkphp succes error跳转模板 设置
执行成功 换成自己想要的模块 下面的路径是默认的模板\ThinkPHP\Tpl\dispatch_jump.tpl D:\wamp\www\ThinkPHP\Tpl\dispatch_jump.tpl ...
- Kafka——分布式消息系统
Kafka——分布式消息系统 架构 Apache Kafka是2010年12月份开源的项目,采用scala语言编写,使用了多种效率优化机制,整体架构比较新颖(push/pull),更适合异构集群. 设 ...
- C-基本语法与运算
编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...