bfs或dfs求图中两点最大的距离
2018-02-06 05:37
330 查看
题目://https://www.hackerearth.com/zh/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/agitated-chandan/description/
Agitated Chandan
Attempted by: 1452
/
Accuracy: 83%
/
Maximum Score: 20
/
26 Votes
Tag(s):
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:
If maximum distance is <100, cost = 0.
If maximum distance is > 100, cost = 100.
If maximum distance is > 1000, cost = 1000.
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
SAMPLE OUTPUT
Time Limit:2.0 sec(s) for each input file.
Memory Limit:256
MB
Source Limit:1024
KB
Marking Scheme:Marks
are awarded when all the testcases pass.
Allowed Languages:C,
C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala,
Swift, Visual Basic
代码:dfs
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll dist[100009];
ll vis[100009];
vector< pair<ll,ll> >adj[100009];
void dfs(ll x)
{
ll v,w;
vis[x]=1;
for(int i=0;i<adj[x].size();i++)
{
v=adj[x][i].first;
w=adj[x][i].second;
if(!vis[v])
{
dist[v]=dist[x]+w;
dfs(v);
}
}
}
int main()
{
ll T,t,n,a,b,c,i,maxi;
cin>>T;
for(int t=1;t<=T;t++)
{
cin>>n;
for(i=0;i<=n;i++)
adj[i].clear();
for(int i=1;i<n;i++)
{
cin>>a>>b>>c;
adj[a].push_back(make_pair(b,c));
adj[b].push_back(make_pair(a,c));
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
dfs(1);
maxi=0;
a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[i];
a=i;
}
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
dfs(a);
maxi=0;
a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[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;
}bfs:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int dist[100001];
int vis[100001];
int n, tme;
struct node {
int i,d;
};
typedef struct node node;
vector<node> adj[100001];
void bfs(int start){
queue<int>q;
q.push(start);
vis[start]=1;
while(!q.empty())
{
int p=q.front();
q.pop();
for(int i=0;i<(int)adj[p].size();i++)
{
if(vis[adj[p][i].i]==0){
vis[adj[p][i].i]=1;
dist[adj[p][i].i]=dist[p]+adj[p][i].d;
q.push(adj[p][i].i);
}
}
}
}
int main()
{
int u,v,w;
node temp;
int test;
cin>>test;
while(test--)
{
cin>>n;
for(int i=1;i<=n;i++) {
adj[i].clear();
}
for(int i=1;i<n;i++)
{
cin>>u>>v>>w;
temp.i=v;
temp.d=w;
adj[u].push_back(temp);
temp.i=u;
adj[v].push_back(temp);
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
bfs(1);
int maxi=0;
int a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[i];
a=i;
}
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
bfs(a);
maxi=0;
a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[i];
a=i;
}
}
if(maxi>10000)
a=10000;
else if(maxi>1000)
a=1000;
else if(maxi>100)
a=100;
else
a=0;
printf("%d %d\n",a,maxi);
}
return 0;
}
Agitated Chandan
Attempted by: 1452
/
Accuracy: 83%
/
Maximum Score: 20
/
26 Votes
Tag(s):
Algorithms, Easy, Graph
Theory
PROBLEMEDITORIAL
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:
If maximum distance is <100, cost = 0.
If maximum distance is > 100, cost = 100.
If maximum distance is > 1000, cost = 1000.
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
SAMPLE OUTPUT
0 8
Time Limit:2.0 sec(s) for each input file.
Memory Limit:256
MB
Source Limit:1024
KB
Marking Scheme:Marks
are awarded when all the testcases pass.
Allowed Languages:C,
C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala,
Swift, Visual Basic
代码:dfs
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll dist[100009];
ll vis[100009];
vector< pair<ll,ll> >adj[100009];
void dfs(ll x)
{
ll v,w;
vis[x]=1;
for(int i=0;i<adj[x].size();i++)
{
v=adj[x][i].first;
w=adj[x][i].second;
if(!vis[v])
{
dist[v]=dist[x]+w;
dfs(v);
}
}
}
int main()
{
ll T,t,n,a,b,c,i,maxi;
cin>>T;
for(int t=1;t<=T;t++)
{
cin>>n;
for(i=0;i<=n;i++)
adj[i].clear();
for(int i=1;i<n;i++)
{
cin>>a>>b>>c;
adj[a].push_back(make_pair(b,c));
adj[b].push_back(make_pair(a,c));
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
dfs(1);
maxi=0;
a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[i];
a=i;
}
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
dfs(a);
maxi=0;
a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[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;
}bfs:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int dist[100001];
int vis[100001];
int n, tme;
struct node {
int i,d;
};
typedef struct node node;
vector<node> adj[100001];
void bfs(int start){
queue<int>q;
q.push(start);
vis[start]=1;
while(!q.empty())
{
int p=q.front();
q.pop();
for(int i=0;i<(int)adj[p].size();i++)
{
if(vis[adj[p][i].i]==0){
vis[adj[p][i].i]=1;
dist[adj[p][i].i]=dist[p]+adj[p][i].d;
q.push(adj[p][i].i);
}
}
}
}
int main()
{
int u,v,w;
node temp;
int test;
cin>>test;
while(test--)
{
cin>>n;
for(int i=1;i<=n;i++) {
adj[i].clear();
}
for(int i=1;i<n;i++)
{
cin>>u>>v>>w;
temp.i=v;
temp.d=w;
adj[u].push_back(temp);
temp.i=u;
adj[v].push_back(temp);
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
bfs(1);
int maxi=0;
int a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[i];
a=i;
}
}
memset(vis,0,sizeof(vis));
memset(dist,0,sizeof(dist));
bfs(a);
maxi=0;
a=0;
for(int i=1;i<=n;i++)
{
if(dist[i]>maxi)
{
maxi=dist[i];
a=i;
}
}
if(maxi>10000)
a=10000;
else if(maxi>1000)
a=1000;
else if(maxi>100)
a=100;
else
a=0;
printf("%d %d\n",a,maxi);
}
return 0;
}
相关文章推荐
- HDU2444 二分图判断(BFS 的染色法) + 求最大匹配边数(DFS 的匈牙利算法)
- 二叉树分析(两点最大距离)
- dfs 例题(寻找最大距离)
- HDU - 5723 E - Abandoned country——最小生成树+dfs求任意两点距离的和
- 某公司2017秋季招聘在线编程题(求圆上n个点任意两点之间最大距离)
- CSUOJ 1219 建食堂(两点之间最大距离最小)
- 【二分图匹配】最大匹配-匈牙利算法BFS && DFS写法
- HDOJ 2063 过山车【匈牙利算法求二分图最大匹配 DFS增广 BFS增广】
- hdu 3938 Portal 计算两点路上最大距离中的最小值<=L的路径个数 离线并查集
- HDU 4460 Friend Chains(任意两点最短路的最大值 bfs或spfa)
- 经典c程序(0038)---矩阵中两点最短距离BFS
- HDU 4460 求任意两点最短距离的最大距离
- DFS&BFS--最大乘积
- Java二叉树排序及任意两点个节点间的最大距离
- HDU 5723 Abandoned country (并查集 + DFS+求解树上任意两点间的距离的平均值)
- POJ-3162 Walking Race (求树上两点之间最大距离)
- POJ 2531 Network Saboteur(DFS分两点集求最大权)
- HDU 1044 Collect More Jewels【BFS+DFS+建立距离图】
- 【二分图|最大匹配】POJ-3041 Asteroids(匈牙利算法dfs、bfs版)
- 【算法学习笔记】36.凸包 求最大两点距离 SJTU OJ 1244 Date A Live