Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求管理员或者大神解答--

Posted by wzpziyi1 at 2014-07-30 20:20:22 on Problem 2288
求帮助,两份ac代码,却对于这两组测试数据输出不同的结果,到底哪个是对的?
数据:
100
8 32
18 17 44 25 65 6 58 3 
3 4
7 1
6 7
2 4
1 6
7 2
1 2
7 6
3 2
3 1
4 2
4 5
1 1
3 1
5 7
3 4
5 7
4 5
2 1
1 2
7 3
3 7
1 1
7 2
7 3
5 5
2 6
3 3
7 6
2 7
3 2
8 1

11 132
21 46 84 66 97 34 91 33 90 40 84 
2 9
11 10
2 8
3 10
7 7
5 1
4 3
9 6
2 3
9 10
6 7
1 7
9 4
11 1
4 10
2 8
4 2
10 10
7 11
7 2
11 7
1 8
2 4
8 9
8 2
1 11
5 9
2 3
8 8
5 1
5 10
3 6
11 2
7 6
10 10
9 1
11 8
7 1
5 1
7 2
7 5
7 3
7 7
3 8
4 7
11 4
2 11
2 8
10 6
9 10
3 10
6 11
11 11
9 2
7 2
9 9
6 4
2 6
10 1
9 11
9 2
9 4
8 7
1 6
7 4
11 9
3 2
5 5
3 8
2 11
11 11
7 4
9 2
8 4
7 1
11 2
5 6
6 3
8 5
2 7
8 1
10 6
10 1
5 1
8 9
5 8
6 10
8 2
9 11
10 11
10 4
10 4
8 7
1 8
2 11
3 3
11 4
9 10
1 1
1 6
11 1
11 4
10 10
5 8
6 9
7 11
9 8
10 8
6 3
11 8
11 7
7 5
7 4
6 7
11 5
11 8
6 4
5 5
6 3
8 11
6 2
5 7
9 6
11 7
6 9
3 4
6 4
2 7
9 4
2 7
2 5
10 5

第一份代码输出:
79469 1
3434406 1

第二份代码输出:
555392 72
4650271 600

第一份代码(别人的):
#include <stdio.h>
#include <string.h>

typedef __int64 i64;

const int maxn = 13;
const int maxs = 1<<maxn | 1;

i64 dp[maxn][maxn][maxs], way[maxn][maxn][maxs];
int map[maxn][maxn], v[maxn];
int n, m, S;

void StateCompressDp()
{

    memset(dp, -1, sizeof(dp));
    memset(way, 0, sizeof(way));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (map[i][j])
            {
                dp[i][j][(1<<i)+(1<<j)] = v[i] + v[j] + v[i]*v[j];
                way[i][j][(1<<i)+(1<<j)] = 1;
            }

        }
    }
    for (int p = 3; p < S; p++)
    {
        for (int i = 0; i < n; i++)
        {
            if (!(p&1<<i)) continue;
            for (int j = 0; j < n; j++)
            {
                if (i == j || !(p&1<<j) || dp[i][j][p] == -1) continue;
                for (int k = 0; k < n; k++)
                {
                    if (p&1<<k || !map[j][k]) continue;
                    int r = p + (1 << k);
                    i64 q = dp[i][j][p] + v[k] + v[j]*v[k];
                    if (map[i][k])
                    {
                        q += v[i] * v[j] * v[k];
                    }
                    if (q > dp[j][k][r])
                    {
                        dp[j][k][r] = q;
                        way[j][k][r] = way[i][j][p];
                    }
                    else if (q == dp[j][k][r])
                    {
                        way[j][k][r] += way[i][j][p];
                    }
                }
            }
        }
    }

    return ;
}

int main ()
{

    int t;

    scanf("%d", &t);
    while (t--)
    {
        int tmpx, tmpy;
        memset(map, 0, sizeof(map));
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &v[i]);
        }
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d", &tmpx, &tmpy);
            map[tmpx-1][tmpy-1] = map[tmpy-1][tmpx-1] = 1;
        }
        S = 1 << n;
        if (n == 1)
        {
            printf("%d %d\n", v[0], 1);
        }
        else
        {
            StateCompressDp();
            i64 ansv = -1, ansp = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j) continue;
                    if (dp[i][j][S-1] > ansv)
                    {
                        ansv = dp[i][j][S-1];
                        ansp = way[i][j][S-1];
                    }
                    else if (dp[i][j][S-1] == ansv)
                    {
                        ansp += way[i][j][S-1];
                    }
                }
            }
            printf("%I64d %I64d\n", ansv == -1 ? 0 : ansv, ansp/2);
        }
    }

    return 0;
}


第二份代码(本人的):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef __int64 ss;
const ss inf=(1<<14);
bool map[20][20],vist[14][14][inf];
ss dp[14][14][inf],n,m,s[20],path[14][14][inf];
vector<ss>vet[20];
struct node
{
    ss num,pre,zt;
};
queue<node>q;
void spfa()
{
    if(!q.empty()) q.pop();

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
             if(map[i][j])
             {
                 int tmp=(1<<i)+(1<<j);
                 if(dp[i][j][tmp]<s[i]+s[j]+s[i]*s[j])
                 {
                     dp[i][j][tmp]=s[i]+s[j]+s[i]*s[j];
                     path[i][j][tmp]=1;
                     if(!vist[i][j][tmp])
                     {
                         vist[i][j][tmp]=true;
                         node p;
                         p.num=j;
                         p.pre=i;
                         p.zt=tmp;
                         q.push(p);
                     }
                 }
             }
        }
    }
    while(!q.empty())
    {
        //printf("111\n");
        node p=q.front();
        q.pop();
        vist[p.num][p.pre][p.zt]=false;
        for(ss j=0;j<vet[p.num].size();j++)
        {
            ss i=vet[p.num][j];
            //printf("%I64d  %I64d\n",p.num,i);
            if(p.zt&(1<<i)) continue;
            //if(i==p.num) continue;
            //if(!map[p.num][i]) continue;
            node p1;
            p1.zt=p.zt|(1<<i);
            p1.num=i;
            p1.pre=p.num;
            ss ans=0;
            ans+=dp[p.num][p.pre][p.zt]+s[i]+s[i]*s[p.num];
            ss tmp=p.pre;
            if(map[i][tmp])
            {
                ans+=s[tmp]*s[i]*s[p.num];
            }
            if(ans>dp[p1.num][p1.pre][p1.zt])
            {
                dp[p1.num][p1.pre][p1.zt]=ans;
                path[p1.num][p1.pre][p1.zt]=path[p.num][p.pre][p.zt];
                if(!vist[p1.num][p1.pre][p1.zt])
                q.push(p1);
                vist[p1.num][p1.pre][p1.zt]=true;
            }
            else if(ans==dp[p1.num][p1.pre][p1.zt])
            path[p1.num][p1.pre][p1.zt]+=path[p.num][p.pre][p.zt];
        }
    }
}
int main()
{
    int text;
    scanf("%d",&text);
    while(text--)
    {
        scanf("%I64d%I64d",&n,&m);
        memset(dp,-1,sizeof(dp));
        memset(map,false,sizeof(map));
        memset(vist,false,sizeof(vist));
        memset(path,0,sizeof(path));
        for(ss i=0;i<=n;i++)
        vet[i].clear();
        for(ss i=0;i<n;i++)
        scanf("%I64d",&s[i]);
        for(ss i=1;i<=m;i++)
        {
            ss a,b;
            scanf("%I64d%I64d",&a,&b);
            a--;
            b--;
            map[a][b]=map[b][a]=true;
            vet[a].push_back(b);
            vet[b].push_back(a);
        }
        if(n==1)
        {
            printf("%I64d 1\n",s[0]);
            continue;
        }
        spfa();
        ss maxn=0,ans=(1<<(n))-1;
        ss tol=0;
        for(ss i=0;i<n;i++)
        {
            for(ss j=0;j<n;j++)
            {
                if(maxn<dp[i][j][ans])
                {
                    tol=path[i][j][ans];
                    maxn=dp[i][j][ans];
                }
                else if(maxn==dp[i][j][ans])
                {
                    tol+=path[i][j][ans];
                }
            }
        }
        if(maxn==0)
        printf("0 0\n");
        else
        printf("%I64d %I64d\n",maxn,tol/2);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator