Gym 100646H You’ll be Working on the Railroad(搜索)

题意就是找一条从0到1的简单路径,费用最小。这里的费用定义如下:
1.路径条数=1,费用就为这条边的费用。
2.路径条数=2,费用就为两条中最小的。
3.路径条数>2,费用等于所有边的费用减去路径中最大和次大的费用。
且要求费用相同的话路径条数最少,路径条数 <script type="math/tex" id="MathJax-Element-46">也</script> 相同的话字典序最小。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ALL(v) (v).begin(), (v).end()
#define lowbit(x) ((x)&(-x))
#define Unique(x) sort(ALL(x)); (x).resize(unique(ALL(x)) - (x).begin())
#define BitOne(x) __builtin_popcount(x)
#define showtime printf("time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define Rep(i, l, r) for (int i = l;i <= r;++i)
#define Rrep(i, r, l) for (int i = r;i >= l;--i)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-8;
const double pi = 4 * atan(1);
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 101;
bool vis[maxn];
vector<int> path, rp;
vector<ii> mp[maxn];
int maxv, weg;
bool cmp(vector<int>& p, vector<int>& q) {
    for (int i = 0, j = 0;i < (int)p.size() && j < (int)q.size();++i, ++j)
        if (p[i] < q[j]) return true;
    return false;
}
void dfs(int u,int max1, int max2, int sum) {
    vis[u] = true;
    path.push_back(u);
    if (u == 1) {
        int res = 0;
        if (path.size() == 2) res = sum;
        else if (path.size() == 3) res = sum - max1;
        else res = sum - max1 - max2;
        if (res < maxv) {
            maxv = res;
            rp = path;
        }else if (res == maxv && path.size() < rp.size()) rp = path;
        else if (res == maxv && path.size() == rp.size() && cmp(path, rp)) rp = path;
    }else {
        for (int i = 0;i < mp[u].size();++i) {
            int v = mp[u][i].first, w = mp[u][i].second;
            if (!vis[v]) {
                ++weg;
                if (w > max1) dfs(v, w, max1, sum + w);
                else if (w > max2) dfs(v, max1, w, sum + w);
                else dfs(v, max1, max2, sum + w);
                --weg;
            }
        }
    }
    vis[u] = false;
    path.pop_back();
}
int main(int argc, const char * argv[])
{    
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    // ios::sync_with_stdio(false);
    // cout.sync_with_stdio(false);
    // cin.sync_with_stdio(false);

    int n, u, v, c;
    while(cin >> n && n) {
        Rep(i, 0, n) mp[i].clear();
        Rep(i, 1, n) {
            scanf("%d%d%d", &u, &v, &c);
            mp[u].push_back(ii(v, c));
            mp[v].push_back(ii(u, c));
        }
        path.clear();
        rp.clear();
        memset(vis, false, sizeof vis);
        maxv = INF;
        dfs(0, 0, 0, 0);
        for (int i = 0;i < rp.size();++i)
            printf("%d ", rp[i]);
        printf("%d\n", maxv);
    }

    // showtime;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KIJamesQi/article/details/52335540
今日推荐