/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Accepted 3ms 384.0 KiB
#2 Accepted 48ms 256.0 KiB
#3 Accepted 5ms 368.0 KiB
#4 Accepted 797ms 288.0 KiB
#5 Accepted 891ms 384.0 KiB
#6 Accepted 858ms 256.0 KiB
#7 Wrong Answer 792ms 356.0 KiB
#8 Wrong Answer 777ms 356.0 KiB
#9 Wrong Answer 704ms 432.0 KiB
#10 Wrong Answer 789ms 376.0 KiB

代码

#include <stdio.h>
#include <iostream>
#include <cstring>
const int maxn  = 22;
using namespace std;
int dist[maxn][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 i = 2; i<n; i++) {
        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 i = 2; i<n; i++) {
        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;
    }
    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 14:55:29
评测时间
2017-11-07 14:55:29
评测机
分数
60
总耗时
5669ms
峰值内存
432.0 KiB