CF1204C

CF1204C-Anna, Svyatoslav and Maps

Meaning of the questions:

Topic Portal
do not want to say, read the title.

solution:

Floyd ran first with the shortest path between the vertices. The p (. 1) added to the answer, and then along the path to the subject sequence traversal, if the answer to the last vertex of the shortest distance from the apex to the current iteration, is less than the distance between two points in the original sequence and, together with the answer p (i-1), and continues to traverse the path traversed after the last add p (m)

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
#define LL long long
#define N 110

int e[N][N],ans[N*N*N];
int p[N*N*N],n,m,cnt;

void floyd() {
    for(int k = 1 ; k <= n ; k++) {
        for(int i = 1 ; i <= n ; i++) {
            for(int j = 1 ; j <= n ; j++) {
                e[i][j] = min(e[i][j],e[i][k] + e[k][j]);
            }
        }
    }
}

int main(){
    scanf("%d",&n);
    getchar();
    for(int i = 1 ; i <= n ; i++) {
        for(int j = 1 ; j <= n ; j++) {
            char t = getchar();
            e[i][j] = (t == '1') ? 1 : INF;
            if(i == j) e[i][j] = 0;
        }
        getchar();
    }
    scanf("%d",&m);
    for(int i = 1 ; i <= m ; i++)
        scanf("%d",&p[i]);
    floyd();
    ans[++cnt] = p[1];
    int dis = 0;
    for(int i = 2 ; i <= m ; i++) {
        dis += e[p[i - 1]][p[i]];
        if(dis > e[ans[cnt]][p[i]]){
            ans[++cnt] = p[i - 1];
            dis = e[ans[cnt]][p[i]];
        }
    }
    ans[++cnt] = p[m];
    printf("%d \n",cnt);
    for(int i = 1 ; i <= cnt ; i++)
        printf("%d ",ans[i]);
    //system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/Repulser/p/11391272.html