@Alpacadh
2019-02-19T15:01:39.000000Z
字数 4999
阅读 630
搜索
#include<bits/stdc++.h>
//#include<iostream>
//#include<algorithm>
//#include<cmath>
//#include<cstring>
//#include<stdio.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=1e4+5;
const double eps=1e-4;
typedef long long ll;
typedef pair<int,ll> pil;
struct node
{
double l,r;
}t[N];
int vis[N];
double w[N];
double ans;
double r;
int n;
int len;
void dfs(int x)
{
if(x==n-1)
{
if(t[len].l+t[len].r-r<eps)
ans=max(ans,t[len].l+t[len].r);
return;
}
for(int i=1;i<=len;i++)
{
if(!vis[i])
{
for(int j=1;j<=len;j++)
{
if(!vis[j]&&i!=j)
{
vis[i]=1,vis[j]=1;
len++;
w[len]=w[i]+w[j];
double dl=w[j]/(w[i]+w[j]),dr=1.0-dl;
t[len].l=max(t[i].l+dl,t[j].l-dr);
t[len].r=max(t[j].r+dr,t[i].r-dl);
dfs(x+1);
vis[i]=0,vis[j]=0,vis[len]=0;
t[len].l=0,t[len].r=0;
len--;
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(t,0,sizeof(t));
memset(w,0,sizeof(w));
memset(vis,0,sizeof(vis));
scanf("%lf%d",&r,&n);
for(int i=1;i<=n;i++)
{
scanf("%lf",&w[i]);
}
len=n;
ans=-1;
dfs(0);
if(ans==-1)
printf("%.f\n",ans);
else
printf("%.8f\n",ans);
}
return 0;
}
(自己写的太复杂了,贴一下别人hash过的代码)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
const int maxs = 20;
const int maxn = 150;
int n,m,cnt,sum;
int deg[maxn],G[maxn][maxn];
int x[maxn],y[maxn],id[maxn][maxn];
int d[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int s[5],t[5];
int dx[] = {0,0,0,-1,1};
int dy[] = {0,1,-1,0,0};
int find_ID(int a,int b,int c){
return (a<<16)|(b<<8)|c;
}//hash操作,将三位数变成一个整数。
bool conflict(int a,int b,int a2,int b2){
if((a2==b && a==b2) || a2==b2) return true;
return false;
}
int bfs(){
queue<int> que,anti_que;
d[s[0]][s[1]][s[2]] = 0;
d[t[0]][t[1]][t[2]] = 0;
que.push(find_ID(s[0],s[1],s[2]));
anti_que.push(find_ID(t[0],t[1],t[2]));
vis[s[0]][s[1]][s[2]] = 1;
vis[t[0]][t[1]][t[2]] = 2;
while(!que.empty() && !anti_que.empty()){
int num = que.size(),anti_num = anti_que.size();
while(num--){
int top = que.front();que.pop();
int a = (top>>16)&0xff,b = (top>>8)&0xff,c = (top&0xff);
for(int i = 0;i < deg[a];i++){
int a2 = G[a][i];
for(int j = 0;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = 0;k < deg[c];k++){
int c2 = G[c][k];
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue;
if(vis[a2][b2][c2] == 0){
d[a2][b2][c2] = d[a][b][c]+1;
vis[a2][b2][c2] = 1;
que.push(find_ID(a2,b2,c2));
}
else if(vis[a2][b2][c2] == 2){
return d[a][b][c]+d[a2][b2][c2]+1;
}
}
}
}
}
while(anti_num--){
int top = anti_que.front();anti_que.pop();
int a = (top>>16)&0xff,b = (top>>8)&0xff,c = (top&0xff);
for(int i = 0;i < deg[a];i++){
int a2 = G[a][i];
for(int j = 0;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = 0;k < deg[c];k++){
int c2 = G[c][k];
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue;
if(vis[a2][b2][c2] == 0){
d[a2][b2][c2] = d[a][b][c]+1;
vis[a2][b2][c2] = 2;
anti_que.push(find_ID(a2,b2,c2));
}
else if(vis[a2][b2][c2] == 1){
return d[a][b][c]+d[a2][b2][c2]+1;
}
}
}
}
}
}
return -1;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d%d\n",&m,&n,&sum) && (m||n||sum)){
char gra[maxs][maxs];
cnt = 0;
memset(d,-1,sizeof(d));
memset(vis,0,sizeof(vis));
for(int i = 0;i < n;i++) fgets(gra[i],maxs,stdin);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(gra[i][j] != '#'){
x[cnt] = i,y[cnt] = j,id[i][j] = cnt;
if(islower(gra[i][j])) s[gra[i][j]-'a'] = cnt;
else if(isupper(gra[i][j])) t[gra[i][j]-'A'] = cnt;
cnt++;
}
}
}
for(int i = 0;i < cnt;i++){
int X = x[i],Y = y[i];
deg[i] = 0;
for(int k = 0;k < 5;k++){
int xx = X+dx[k],yy = Y+dy[k];
if(gra[xx][yy] != '#') G[i][deg[i]++] = id[xx][yy];
}
}
if(sum <= 2){
s[2] = t[2] = G[cnt][0] = cnt;
deg[cnt++] = 1;
}
if(sum <= 1){
s[1] = t[1] = G[cnt][0] = cnt;
deg[cnt++] = 1;
}
printf("%d\n",bfs());
}
return 0;
}
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=10;
const double eps=1e-4;
typedef long long ll;
typedef pair<int,ll> pil;
int a[N];
int n;
bool check()
{
for(int i=0;i<n-1;i++)
if(a[i]>a[i+1])
return false;
return true;
}
int h()
{
int cnt=0;
for(int i=0;i<n-1;i++)
{
if(a[i]+1!=a[i+1])
cnt++;
}
if(a[n-1]!=n)
cnt++;
return cnt;
}
int dfs(int d,int dep)
{
if(3*d+h()>3*dep)
return 0;
if(check())
return 1;
int b[N],tep[N];
memcpy(tep,a,sizeof(a));
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int cnt=0;
for(int k=0;k<n;k++)
{
if(k<i||k>j)
{
b[cnt++]=a[k];
}
}
for(int k=0;k<=cnt;k++)
{
int cnt1=0;
for(int kk=0;kk<k;kk++)
a[cnt1++]=b[kk];
for(int kk=i;kk<=j;kk++)
a[cnt1++]=tep[kk];
for(int kk=k;kk<cnt;kk++)
a[cnt1++]=b[kk];
if(dfs(d+1,dep))
return 1;
memcpy(a,tep,sizeof(tep));
}
}
}
return 0;
}
int main()
{
int tot=1;
while(scanf("%d",&n))
{
if(n==0)
break;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int ans=5;
if(check())
ans=0;
else
{
for(int i=1;i<5;i++)
{
if(dfs(0,i))
{
ans=i;
break;
}
}
}
printf("Case %d: %d\n",tot++,ans);
}
return 0;
}