Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

最普通的深度遍历算法答案,不想看答案者慎入!!!

Posted by sui1999 at 2011-09-15 19:49:40 on Problem 1062
// 9319497	sui1999	1062	Accepted	1032K	63MS	G++	1465B	2011-09-15 19:47:34
#include <iostream>
#include <fstream>
#include <cstring>

#define MAX_NUMS 200
#define MAX_INT     0x7FFFFFFF
#define abs(a) (a) > 0 ? (a) : -(a)

using namespace std;

int         graph  [MAX_NUMS][MAX_NUMS][2], m, n, p, l, x, t, v, sum, result, history[MAX_NUMS], history_size;
bool      visited [MAX_NUMS];

bool check(int index)
{
    for(int i = 0; i < history_size; i++)
        if(graph[index][index][1] - history[i] < -m || graph[index][index][1] - history[i] > m)
            return false;

    return true;
}

void dfs(int index, int temp)
{
    visited[index] = true;
    history[history_size++] = graph[index][index][1];

    if(graph[index][index][0] + temp < sum)
        sum = graph[index][index][0] + temp;

    for(int i = 0; i < n; i++)
    {
        if(!visited[i] && graph[index][i][0] && check(i))
            dfs(i, temp + graph[index][i][0]);
    }

    history_size--;
    visited[index] = false;
}

int main()
{
    //std::ifstream cin("case_in");

    memset(graph, 0, sizeof(graph)), sum = MAX_INT;
    for(int i = 0; i < MAX_NUMS; i++)
        visited[i] = false;

    cin >> m >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> p >> l >> x, graph[i][i][0] = p, graph[i][i][1] = l;

        for(int j = 0; j < x; j++)
            cin >> t >> v, graph[i][t-1][0] = v;
    }

    dfs(0, 0);

    std::cout << sum << std::endl;

    //cin.close();

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator