Description
经过数月的准备,小天狼星,一个被诬陷的杀人犯,准备闯入霍格沃茨见见他的侄子。霍格沃茨的地图呈一颗树状分布。每个房间由若干跳过道通向其他房间。由于小天狼星想尽快找到哈利:
0.他会从房间0开始找
1.他总是会选择离自己最近的房间找
2.如果没找到,则继续选最近的房间深入
3.如果已没有房间可走,则返回到上一个房间,继续选择(往回走也算时间哦)。
4.当然,除了往回走,小天狼星是不会去一个房间两次的。
Input
第1行,nn房间个数,pp哈利所在的房间。(p≤n<100)
第2∼n2∼n行,每行3个整数s,t,ls,t,l。从房间ss到房间tt的时间ll。(s≠t, 0≤s<t<n, 0<l≤100000<l≤10000)
Output
输出找到哈利的时间(开始时间为0)。
思路:一看就是个先序遍历,不过注意往回走的时候也要累加,注意标记。
虽然数据范围只有100.但是我自己用map 的时候出了一个bug,后来是每次vectorsort后遍历的。复杂度肯定没有map的优秀。
讲讲我用map产生的bug吧:
我想方便一点不写重载,然后map<int,int>map1[maxn],因为默认当第一关键字排序,所以我把距离放第一个关键字了.
但是第一个是map的key,insert的话如果key存在,不再插入;[]的话key存在会修改,所以导致距离作为关键字排序产生了问题。
所以放第二关键字,对map的排序重载。
这里的代码就暴力的每次sort
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=200;
typedef long long LL;
typedef pair<LL,LL>P;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
///map<LL,LL>g[maxn];
vector<P>g[maxn];
LL ans=0;
LL n,p;
bool flag=1;
void dfs(LL u,LL fa,LL fee){
///debug(u);
if(u==p){
flag=0;
return;
}
sort(g[u].begin(),g[u].end());
for(auto i:g[u]){
LL v=i.second;LL cost=i.first;
if(v==fa||flag==0) continue;
ans+=cost;
dfs(v,u,cost);
}
if(flag) ans+=fee;
}
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
cin>>n>>p;
p++;
for(LL i=1;i<n;i++){
LL s,t,l;cin>>s>>t>>l;
s++;t++;
g[s].push_back({l,t});
/// g[s].insert({l,t});
/// g[t].insert({l,s});
}
dfs(1,0,0);
/*
for(LL i=1;i<=n;i++){
for(auto j:g[i]){
cout<<"连到的点"<<j.second<<"的花费"<<j.first<<" ";
cout<<"\n";
}
}*map吞边了???
cout<<ans<<endl;
return 0;
}