为边分配权重,以使权重方面的最长路径最小化
给定树的边缘和总和 S。任务是为树的所有边缘分配权重,以使权重方面的最长路径最小化,分配的权重总和应为 S 并打印最长路径重量。
注意:边缘可以在 [0, S] 范围内分配任何权重,也可以是分数。
例子:
Input: 1
/ | \
2 3 4
S = 3
Output: 2
All the edges can be assigned weights of 1, so
the longest path will in terms of weight will be
2--1--4 or 2--1--3
Input: 1
/
2
/ \
3 4
/ \
5 6
S = 1
Output: 0.50
Assign the given below weights to edges.
1--2: 0.25
2--3: 0.25
2--4: 0
4--5: 0.25
4--6: 0.25
Hence the longest path in terms of weight is 1--2--3
or 1--2--4--5 or 1--2--4--6.
方法:一棵树的属性,即一条路径中最多可以有两个叶子节点,可以用来解决上述问题。因此,如果我们只为连接叶节点的边分配权重,而将其他边分配为 0。那么连接到叶节点的每条边都将被分配 s/(叶节点数)。由于一条路径最多可以包含两个叶子节点,因此最长的路径将是2 * (s/叶子节点数)。
下面是上述方法的实现:
C++
// C++ program to assign weights to edges to
// minimize the longest path in terms of weight
#include
using namespace std;
// Function to add edges
void addEdges(int u, int v, vector adj[])
{
adj[u].push_back(v);
adj[v].push_back(u);
}
// Function that assigns weights
// and returns the longest path
long double longestPath(vector adj[],
int s, int n)
{
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (adj[i].size() == 1)
cnt++;
}
long double ans =
2.0 * (long double)(s / (long double)(cnt));
return ans;
}
// Driver Code
int main()
{
int n = 4;
// Create an adjacency list
// to store tree
vector adj[n + 1];
// Add edges
addEdges(1, 2, adj);
addEdges(1, 3, adj);
addEdges(1, 4, adj);
// Given Sum
int s = 3;
// Function that prints the
// longest path in terms of weights
cout << longestPath(adj, s, n);
}
Java
// Java program to assign weights to edges to
// minimize the longest path in terms of weight
import java.util.*;
class GFG
{
// Function to add edges
static void addEdges(int u, int v, Vector adj[])
{
adj[u].add(v);
adj[v].add(u);
}
// Function that assigns weights
// and returns the longest path
static double longestPath(Vector adj[], int s, int n)
{
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (adj[i].size() == 1)
cnt++;
}
double ans = 2.0 * (double) (s / (double) (cnt));
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
// Create an adjacency list
// to store tree
Vector[] adj = new Vector[n + 1];
for (int i = 0; i < n + 1; i++)
adj[i] = new Vector();
// Add edges
addEdges(1, 2, adj);
addEdges(1, 3, adj);
addEdges(1, 4, adj);
// Given Sum
int s = 3;
// Function that prints the
// longest path in terms of weights
System.out.print(longestPath(adj, s, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to assign weights to
# edges to minimize the longest path
# in terms of weight
# Function to add edges
def addEdges(u, v, adj):
adj[u].append(v)
adj[v].append(u)
# Function that assigns weights
# and returns the longest path
def longestPath(adj, s, n):
cnt = 0
for i in range(1, n + 1):
if len(adj[i]) == 1:
cnt += 1
ans = 2 * (s / cnt)
return ans
# Driver Code
if __name__ == "__main__":
n = 4
# Create an adjacency list
# to store tree
adj = [[] for i in range(n + 1)]
# Add edges
addEdges(1, 2, adj)
addEdges(1, 3, adj)
addEdges(1, 4, adj)
# Given Sum
s = 3
# Function that prints the
# longest path in terms of weights
print(longestPath(adj, s, n))
# This code is contributed by Rituraj Jain
C#
// C# program to assign weights to edges to
// minimize the longest path in terms of weight
using System;
using System.Collections.Generic;
class GFG
{
// Function to add edges
static void addEdges(int u, int v, List []adj)
{
adj[u].Add(v);
adj[v].Add(u);
}
// Function that assigns weights
// and returns the longest path
static double longestPath(List []adj, int s, int n)
{
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (adj[i].Count == 1)
cnt++;
}
double ans = 2.0 * (double) (s / (double) (cnt));
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int n = 4;
// Create an adjacency list
// to store tree
List[] adj = new List[n + 1];
for (int i = 0; i < n + 1; i++)
adj[i] = new List();
// Add edges
addEdges(1, 2, adj);
addEdges(1, 3, adj);
addEdges(1, 4, adj);
// Given Sum
int s = 3;
// Function that prints the
// longest path in terms of weights
Console.Write(longestPath(adj, s, n));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
2