dfs -cogs 5 P provided service point

Topic links: http://cogs.pro:8081/cogs/problem/problem.php?pid=FSXJmiJSg

 

Problem Description
In order to further popularize nine-year compulsory education, the government should establish a P Hope primary school in a township, the township a total of n villages, the distance between the villages is known, will build schools in villages where P is best? (S benchmark is the nearest school students, that students come to school, the students go to the farthest distance standard. Or farthest distance students and schools as small as possible.)

 
[Input Format]
Input is composed of rows, the first row there are three integers, n- ( . 1 n- 100 ) , m ( . 1 m n- 2 ) , P ; n- represents the number of villages, m represents a number between village road. Of 2 to m + . 1 line is the information of each path, each row of three integers, inter start, end and two village road distance. (Village numbered from 0)
[Output format]
P integers, the village where the school number (if P or more villages are suitable for the establishment of schools, choose a small number of P villages build schools, according to numbers from small to large output output).
[Sample input]
Enter the file name: djsc.in
6 8 2
0 2 10
0 4 30
0 5 100
1 2 5
2 3 50
3 5 10
4 3 20
4 5 60
[Sample Output]
Output File Name: djsc.out
0 3
 
 
Ideas: When students focus on the farthest distance from the school as small as possible, simultaneously press the number from small to large output, try to put the first p-point, and then update, search violence reached a point p (x == p + 1), carried out for each village to check, find the nearest the school, and then updates the maximum minimum distance, the maxDis is the farthest distance from the school's students, seeking its as small as possible, as the update is set ans variable, when find ans is smaller than the farthest distance the school from students, update the res array that is the answer.
 
Code:
#define _CRT_SECURE_NO_WARNINGS 
#include <the iostream> 
#include <cstdio> 
#include <the cmath> 
#include <algorithm> 
#include <CString>
 #define MAXN 110
 the using  namespace STD;
 int Edge [MAXN] [MAXN];
 int RES [MAXN] ;
 int TEMP [MAXN]; // temporary array 
int n-, m, P, maxDis, minDis = 99999999 , ANS = 99999999 ; 

void DFS ( int X, int now)    // X to represent an array of several Riga, now represents plus points 
{
    IF (X == P + . 1 ) 
    { 
        maxDis = 0 ;
         for ( int I = 0 ; I <n-; I ++ ) 
        { 
            minDis = 99999999 ;
             for ( int J = . 1 ; J <= P; J ++ ) 
            { 
                minDis = min (minDis, Edge [i] [the TEMP [J]]);   // find the distance p i village school recently that the school's 
            } 
            maxDis = max (maxDis, minDis);          // find the furthest distance after update 
        }
        if (ans > maxDis)                        //最远的学生走的路最少
        {
            ans = maxDis;
            memset(res, 0, sizeof(res));
            for (int i = 0; i < n; i++)
                res[i] = temp[i];
        }
        return;
    }
    for (int i = now + 1; i < n; i++)
    {
        temp[x] = i;
        dfs(x + 1, i);
    }
}

int main()
{
    freopen("djsc.in", "r", stdin);
    freopen("djsc.out", "w", stdout);
    int x, y, v;
    scanf("%d %d %d", &n, &m, &p);
    for(int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            edge[i][j] = 99999999;
            edge[i][i] = 0;
        }
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d%d", &x, &y, &v);
        edge[x][y] = edge[y][x] = v;
    }
    for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                edge[i][j] = min(edge[i][j], edge[i][k] + edge[k][j]);
    dfs(1, -1);
    for (int i = 1; i <= p; i++)
        printf("%d ", res[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/kxxy/p/11731731.html