/ Randle /

记录详情

Time Exceeded


  
# 状态 耗时 内存占用
#1 Accepted 4ms 328.0 KiB
#2 Accepted 84ms 256.0 KiB
#3 Accepted 6ms 356.0 KiB
#4 Time Exceeded ≥1007ms ≥344.0 KiB
#5 Time Exceeded ≥1001ms ≥256.0 KiB
#6 Wrong Answer 971ms 256.0 KiB
#7 Time Exceeded ≥1002ms ≥360.0 KiB
#8 Wrong Answer 958ms 384.0 KiB
#9 Wrong Answer 930ms 364.0 KiB
#10 Time Exceeded ≥1001ms ≥512.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>20000000) 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>20000000) 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:04
评测时间
2017-11-07 15:38:04
评测机
分数
30
总耗时
≥6969ms
峰值内存
≥512.0 KiB