PAT甲级-1087 All Roads Lead to Rome (30 分)

题目:1087 All Roads Lead to Rome (30 分)
分析:根据题意DFS即可,只不过城市名是字符串,用map。
#include <iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#include<iostream>
using namespace std;
#define MAX 999999999
typedef long long ll;
int n,m,k;
map<string,int>hap;
map<string,int>visi;
map<string,map<string,int>>road;
map<string,vector<string>>city;
int min_cost = MAX;
int max_hap = -1;
int num;
vector<string>ans;
double avg_hap;
int c_cost ;
int c_hap ;
int c_num;
vector<string>c_ans;
void dfs(string root)
{
    visi[root] = 1;
    if(root == "ROM" ){
        if(c_cost < min_cost){
            min_cost = c_cost;
            max_hap = c_hap;
            num = c_num;
            ans = c_ans;
        }
        else if(c_cost == min_cost && c_hap > max_hap){
            max_hap = c_hap;
            num = c_num;
            ans = c_ans;
        }
        else if(c_cost == min_cost && c_hap == max_hap && c_num < num){
            num = c_num;
            ans = c_ans;
        }
    }
    else{
        for(int i = 0; i < city[root].size();i++)
        {
            if(!visi[city[root][i]] )
            {
                c_cost += road[root][city[root][i]];
                c_num++;
                c_hap += hap[city[root][i]];
                c_ans.push_back(city[root][i]);
                dfs(city[root][i]);
                c_cost -= road[root][city[root][i]];
                c_num--;
                c_hap -= hap[city[root][i]];
                c_ans.pop_back();
            }
        }
    }
    visi[root] = 0;
}
int cnt;
void dfs2(string root)
{
    visi[root] = 1;
    if(root == "ROM" && c_cost == min_cost)
            cnt++;
    else{
        for(int i = 0; i < city[root].size();i++)
        {
            if(!visi[city[root][i]] )
            {
                c_cost += road[root][city[root][i]];
                c_num++;
                c_hap += hap[city[root][i]];
                c_ans.push_back(city[root][i]);
                dfs2(city[root][i]);
                c_cost -= road[root][city[root][i]];
                c_num--;
                c_hap -= hap[city[root][i]];
                c_ans.pop_back();
            }
        }
    }
    visi[root] = 0;
}
int main()
{
    string sta;
    cin>>n>>m>>sta;
    for(int i = 0; i < n - 1; i ++)
    {
        string x;int p;
        cin>>x>>p;
        hap[x] = p;
    }
    for(int i = 0 ; i < m ; i++)
    {
        string a,b;
        int x;
        cin>>a>>b>>x;
        road[a][b] = road[b][a] = x;
        city[a].push_back(b);
        city[b].push_back(a);
    }
    dfs(sta);
    dfs2(sta);
    cout<<cnt<<" "<<min_cost<<" "<<max_hap<<" "<<max_hap/num<<endl;
    cout<<sta;
    for(int i = 0 ; i < ans.size();i++)
        cout<<"->"<<ans[i];
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值