/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Accepted 3ms 340.0 KiB
#2 Accepted 84ms 256.0 KiB
#3 Accepted 6ms 384.0 KiB
#4 Wrong Answer 580ms 352.0 KiB
#5 Wrong Answer 536ms 344.0 KiB
#6 Wrong Answer 533ms 440.0 KiB
#7 Wrong Answer 583ms 352.0 KiB
#8 Wrong Answer 510ms 256.0 KiB
#9 Wrong Answer 499ms 336.0 KiB
#10 Wrong Answer 565ms 384.0 KiB

代码

#include <stdio.h>
#include <iostream>
#include <cstring>
const int maxn  = 22;
using namespace std;
int dist[maxn][maxn];
int randArr[maxn];
int opt[maxn],visit[maxn],visit2[maxn];
int steps[maxn];
int n,m;
int dista,dista2,ans;
int opt22[maxn];
long long times = 0;
void dfs2(int a) {
    times++;
    if(times>10000000) return;
    int visitall = 1;
    for(int j = 1; j<=20; j++) {
        if(randArr[j]<=n-1&&randArr[j]>=2) {
            int i = randArr[j];
            if(visit2[i]==0) {
                visitall = 0;
                visit2[i] = 1;
                opt22[a] = i;
                dfs2(i);
                opt22[a] = 0;
                visit2[i] = 0;
            }
        }
    }
    if(visitall) {
        int dista22 = 0;
        int z = n;
        int i = 0;
        int flag = 1;
        while(opt22[z]) {
            dista22+=dist[z][opt22[z]];
            ++i;
            if(steps[opt22[z]]<=(n-2)/2&&i>(n-2)-(n-2)/2) {
                flag = 0;
                break;
            }
            z=opt22[z];
        }
        if(flag) {
            dista22+=dist[z][1];
            dista2 = min(dista2,dista22);
        }
    }
}
void dfs(int a) {
    times++;
    if(times>10000000) return;
    int visitall = 1;
    for(int j = 1; j<=20; j++) {
        if(randArr[j]<=n-1&&randArr[j]>=2){
            int i = randArr[j];
            if(visit[i]==0) {
                visitall = 0;
                visit[i] = 1;
                opt[a] = i;
                dfs(i);
                opt[a] = 0;
                visit[i] = 0;
            }
        }
    }
    if(visitall) {
        memset(visit2,0,sizeof(visit2));
        dista= 0;
        int z = 1;
        int i = 0;
        while(opt[z]) {
            dista+=dist[z][opt[z]];
            steps[opt[z]]  = ++i;
            z=opt[z];
        }
        dista+=dist[z][n];
        if(dista<ans) {
            dista2 = 1e9;
            dfs2(n);
            ans = min(ans,dista+dista2);
        }
    }
}
int main() {
    ans = 1e9;
    cin>>n>>m;
    for(int i = 1; i<=n; i++)for(int j = 1; j<=n; j++) dist[i][j] = 1e9;
    for(int i = 1; i<=n; i++) dist[i][i] = 0;
    for(int i = 1; i<=m; i++) {
        int x,y,z;
        cin>>x>>y>>z;
        x++,y++;
        dist[x][y] = dist[y][x] =z;
    }
    randArr[1] = 5,randArr[2] = 1,randArr[3] = 4,randArr[6] = 15,randArr[7] = 14,randArr[5] = 3,randArr[8] = 19,randArr[9] = 18,randArr[10] = 2;
    randArr[11]  =6,randArr[12] = 7,randArr[13] = 8,randArr[14] = 9,randArr[15] = 10,randArr[16] = 11,randArr[17] = 12,randArr[18] = 13;
    randArr[19] = 16,randArr[20] = 20,randArr[4] = 17;
    for(int k = 1; k<=n; k++) {
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=n; j++)
                dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
        }
    }
    visit[1] = 1;
    dfs(1);
    cout<<ans;
    return 0;
}

信息

递交者
类型
递交
题目
香子兰 T3
题目数据
下载
语言
C++
递交时间
2017-11-07 15:38:24
评测时间
2017-11-07 15:38:24
评测机
分数
30
总耗时
3904ms
峰值内存
440.0 KiB