[luogu2210] Haywire
生活随笔
收集整理的這篇文章主要介紹了
[luogu2210] Haywire
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意
Here
思考
為了 (NOIP) 前練習玄學算法模擬退火,于是 (A) 了這道 黑題,這題正解好像是 (A^*?)
先隨機一波排列,然后隨機兩頭牛來交換位置進行退火,但總之這是個玄學看臉算法(霧)
代碼
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x * f;
}
typedef double D;
const int N = 30;
int n, ANS, sum, f[N][4], ans[N], pos[N];
int calc(){
int ans = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=3; j++){
ans += abs( pos[i] - pos[f[i][j]] );
}
}
return ans;
}
void SA(){
D t = 10000;
while(t > 1e-14){
int x, y;
do{
x = rand() % n + 1, y = rand() % n + 1;
}while(x == y);
swap(pos[x], pos[y]);
int nowans = calc();
if(nowans < ANS){
ANS = nowans;
}
else {
if( exp( (ANS - nowans) / t ) > (double) rand() / RAND_MAX ){
swap(pos[x], pos[y]);
}
}
t *= 0.99;
}
}
int main(){
srand(time(0));
n = read();
for(int i=1; i<=n; i++) ans[i] = pos[i] = i;
for(int i=1; i<=n; i++)
for(int j=1; j<=3; j++) f[i][j] = read();
ANS = calc();
int len = 200;
while(len --) SA();
cout << ANS / 2;
return 0;
}
總結
注意調參
話說有一個問題:
我在洛谷提交的時候一直爆零,是因為我構造了一個 (random) 函數,是這樣的:
int random(int x){
return rand() * rand() % x + 1;
}
但是一直 (WA),只要直接 (rand()) 就過了,不知道為啥,如果知道的同學可以告訴我一聲 (QWQ) 感謝~
總結
以上是生活随笔為你收集整理的[luogu2210] Haywire的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ADO接口简介
- 下一篇: 前端性能测试工具Chrome perfo