# make_pair的用法以及替代make_pair的结构体以及广度优先搜索

1 篇文章 0 订阅
13 篇文章 0 订阅
14 篇文章 0 订阅

Agitated Chandan
Attempted by:  1308
/
Accuracy:  83%
/
Maximum Score:  20
/

Tag(s):

## Algorithms, Easy, Graph Theory

PROBLEM
EDITORIAL
MY SUBMISSIONS
ANALYTICS

Chandan is a horrendous murderer and he wants to kill Arjit just because he's lazy. Chandan is following the trail of Arjit's shoes. The trail is in the form of a k-ary tree. Arjit is lazy, sure, but he's smart. Initially, Arjit and Chandan are standing together, but Arjit magically moves away from Chandan as far as he can go.

Chandan doesn't know the way out, but he knows that Arjit has managed to travel the maximum distance he can. Help Chandan find out the maximum distance he would have to travel to find Arjit. And also tell him how much will he have to pay to travel so far. The travel rates are:

1. If maximum distance is <100, cost = 0.
2. If maximum distance is > 100, cost = 100.
3. If maximum distance is > 1000, cost = 1000.
4. If maximum distance is > 10000, cost = 10000.

Input format:
First line contains the total number of test cases. Then, the next line contains the number of nodes. The the next n-1 lines contain three integers - the first two denote an edge between a and b, the third integer denotes the weight of that edge.

Output format:
You've to print the money Chandan will pay and the maximum distance he will have to travel.

Constraints:
1 <= Test Cases <= 10
2 <= n <= 100000
1 <= a, b <= n
1 <= weight <= 100

SAMPLE INPUT

1
5
1 2 4
3 2 3
2 5 2
4 1 1

#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define write(b) freopen("b.txt","w",stdout)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define maxall(v) *max_element(all(v))
#define minall(v) *min_element(all(v))
#define pb push_back
#define mk make_pair
#define REV(x) reverse(x.begin(),x.end())
#define SORT(v) sort(all(v))
#define UN(v) SORT(v), (v).earse(unique(all(v)),v.end())
#define common(a,b) SORT(a), SORT(b), a.erase(set_intersection(all(a),all(b),a.begin()),a.end())
#define uncommon(a,b) SORT(a), SORT(b), a.erase(set_symmetric_difference(all(a),all(b),a.begin()),a.end())
#define FILL(a,d) memset(a,d,sizeof(a))
#define LL long long
#define PI 2*acos(0.0)
#define pi pair<int,int>
#define MAXM 2147483647
#define MAXML 9223372036854775807LL
#define MODM 1000000007
int binarySearch(vector < int > arr, int l, int r, int x) { while (l <= r){int m = l + (r-l)/2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; return -1; }}
LL gcd(LL a, LL b){if(a==0)return(b);else return(gcd(b%a,a));}
LL fastpow(LL a, LL n, LL temp){if(n==0) return(1);if(n==1)return((a*temp)%MODM); if(n&1)temp=(temp*a)%MODM;return(fastpow((a*a)%MODM,n/2,temp));}
int dist[100001];
int visited[100001];
int n, tme;
struct node {
int i,d;
};
typedef struct node node;
vector<node> v[100001];
int bfs (int start) {
queue<int> q;
q.push(start);
for(int i=0;i<=n;i++){
visited[i]=0;
dist[i]=0;
}
visited[start]=1;
while(!q.empty()){
int temp = q.front();
q.pop();
for(int i=0;i<(int)v[temp].size();i++){
if(visited[v[temp][i].i]==0){
visited[v[temp][i].i]=1;
dist[v[temp][i].i]+=dist[temp]+v[temp][i].d;
q.push(v[temp][i].i);
}
}
}
return int(max_element(dist+1,dist+n+1)-dist);
}
int main() {
int u1,v1,d;
node temp;
int tc;
scanf("%d",&tc);
while(tc--){
scanf("%d",&n);
for(int i=1;i<=n;i++) {
v[i].clear();
}
for(int i=1;i<=n-1;i++) {
scanf("%d%d%d",&u1,&v1,&d);
temp.i = v1;
temp.d = d;
v[u1].push_back(temp);
temp.i = u1;
v[v1].push_back(temp);
}
int start = bfs(1);
int ans = bfs(start);
int finalans = dist[ans];
int money = 0;
if (finalans>100)
money = 100;
if (finalans>1000)
money = 1000;
if (finalans>10000)
money = 10000;
printf("%d %d\n",money,finalans);
}
return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int

vector< pair<ll,ll> >graph[100009];
bool visit[100009];
ll dis[100009];

void dfs(ll u)
{
ll v,i,w;
visit[u]=true;
for(i=0;i<graph[u].size();i++)
{
v=graph[u][i].first;
w=graph[u][i].second;
if(!visit[v])
{
dis[v]=dis[u]+w;
dfs(v);
}
}
}

int main()
{
ll T,t,n,a,b,c,i,maxi;
scanf("%lld",&T);
for(t=1;t<=T;t++)
{
scanf("%lld",&n);
for(i=0;i<=n;i++)
graph[i].clear();
for(i=1;i<n;i++)
{
scanf("%lld %lld %lld",&a,&b,&c);
graph[a].push_back(make_pair(b,c));
graph[b].push_back(make_pair(a,c));
}
memset(visit,false,sizeof visit);
memset(dis,0,sizeof dis);
dfs(1);
maxi=0;
a=0;
for(i=1;i<=n;i++)
{
if(dis[i]>maxi)
{
maxi=dis[i];
a=i;
}
}
memset(visit,false,sizeof visit);
memset(dis,0,sizeof dis);
dfs(a);
maxi=0;
a=0;
for(i=1;i<=n;i++)
{
if(dis[i]>maxi)
{
maxi=dis[i];
a=i;
}
}
if(maxi>10000)
a=10000;
else if(maxi>1000)
a=1000;
else if(maxi>100)
a=100;
else
a=0;
printf("%lld %lld\n",a,maxi);
}
return 0;
}

08-05 8145
03-08 5596
10-13 1万+
04-25 2774
10-27 1332
12-05 1527
05-12 12万+
11-17 6万+
12-18 5768
02-06 685

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

_TianZhirui

¥2 ¥4 ¥6 ¥10 ¥20

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