The 37th ACM/ICPC Asia Regional ChangChun Site Online Contest - K USACO ORZ

      本题暴力+小剪枝枚举3^15种情况 ( 15个长度分别加到3条中的哪个 ) ... 关键是如何判断相同的三角形...我是对每个三角形构造唯一值..再用set容器判断是否出现...


Program:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define ll long long
#define oo 1000000000
#define pi acos(-1)
using namespace std;    
int T,n,a[17],m,sum; 
set<ll> myset;
void DFS(int i,int x1,int x2,int x3)
{
     ll k,a1,a2,a3;
     if (i>n) return;
     if (x1+x2>x3 && x2+x1>x3 && x2+x3>x1)
     {
              a1=x1; a2=x2; a3=x3;
              if (a2>a3)
              {
                    k=a2;
                    a2=a3;
                    a3=k;
              }
              if (a1>a2)
              {
                    k=a2;
                    a2=a1;
                    a1=k;
              }
              if (a2>a3)
              {
                    k=a2;
                    a2=a3;
                    a3=k;
              }              
              k=a1+a2*sum+a3*sum*sum;
              if (myset.find(k)==myset.end())
              {
                     m++;
                     myset.insert(k);
              }
     }
     if (x3-a[i]+x2>x1+a[i])
        DFS(i+1,x1+a[i],x2,x3-a[i]);
     if (x3-a[i]+x1>x2+a[i])
        DFS(i+1,x1,x2+a[i],x3-a[i]);
     DFS(i+1,x1,x2,x3);
} 
int main()
{ 
     freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);
     int t,i;
     scanf("%d",&t);
     while (t--)
     {      
            scanf("%d",&n);
            sum=0;
            for (i=1;i<=n;i++) 
            {
                   scanf("%d",&a[i]);
                   sum+=a[i];
            }
            m=0;  myset.clear();
            DFS(1,0,0,sum); 
            printf("%d\n",m);
     }
     return 0;
}


你可能感兴趣的:(OO)