G - Agri-Net

本文介绍了一种通过最小化光纤使用量来连接多个农场的算法解决方案,旨在帮助 Farmer John 实现其竞选承诺,即为所有农场提供互联网接入。该方案利用了图论中的最小生成树概念,通过输入各农场之间的连接成本矩阵,输出连接所有农场所需的最短光纤总长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

G - Agri-Net

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
The distance between any two farms will not exceed 100,000. 
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output
28
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX=1e4+10;
int a[MAX][MAX];
int father[MAX];
void init(int n)
{
	for( int i = 0 ; i <= n ; i ++)
	  {
	  	   father[i] = i;
	  }
}
int find (int x)
{
	   if(x != father[x])
	   {
	   	 return father[x] = find(father[x]);
	   }
	  return x;
}
int unite(int a,int b)
{
	  int fa = find(a);
	  int fb = find(b);
	  if( fa != fb)
	  {
	  	   father[fa] = fb;
	  	   return 1;
	  }
	  return 0;
}
struct node 
{
	int st;
	int endd;
	int cost;
}p[MAX];
bool cmp(node a,node b)
{
	return a.cost<b.cost;
}
int main()
{

   	  int n;
   	  while(~scanf("%d",&n))
   	  {
   	  init(n);
   	  int ans=-1;
   	for(int i = 1 ; i <= n ; i++)
   	       for(int j = 1 ; j <= n ; j++)
   	      { 
   	              scanf("%d",&a[i][j]);
   	              if(i<j)
   	              {   
   	                    ans++;
   	              	    p[ans].st = i;
   	              	    p[ans].endd=j;
   	              	    p[ans].cost = a[i][j];
   	              	    
					 }
		 }
		 sort (p , p+ans+1,cmp);
		 int t=0  , sum=0;
		 for(int i=0 ; i<= ans; i++)
		 {
		 	  if(unite(p[i].st,p[i].endd))
		 	  {
		 	  	  sum += p[i].cost;
		 	  	  t++;
			   }
		 	if(t == n-1)
		 	break;
		 }
   	     printf("%d\n",sum);
 }	
	
	return 0;
}



 

你遇到的错误是: ``` TypeError: argument of type 'GeneralGraph' is not iterable ``` 发生在这一行代码中: ```python if node not in G_nx: ``` --- ## ❓ 错误原因 你尝试使用 `node not in G_nx` 来判断某个节点是否在图中,但你的 `G_nx` **并不是一个 `networkx.Graph` 或 `networkx.DiGraph`**,而是一个 `causaldag.core.core_utils.GeneralGraph` 类型的对象。 `GeneralGraph` 对象 **不支持 `in` 操作**,因此报错。 --- ## ✅ 解决方案 ### ✅ 方法一:如果你使用的是 `causaldag` 的 `GeneralGraph` 你可以使用 `G_nx.nodes` 来获取节点集合: ```python if node not in G_nx.nodes: G_nx.add_node(node) ``` 或者更稳妥的方式是: ```python existing_nodes = set(G_nx.nodes) for node in range(nodes): if node not in existing_nodes: G_nx.add_node(node) ``` --- ### ✅ 方法二:如果你希望使用 `networkx` 图进行绘图 你可以将 `GeneralGraph` 转换为 `networkx` 图: ```python from causaldag.utils.networkx_utils import general_graph_to_nx nx_graph = general_graph_to_nx(G_nx) # 然后你可以正常使用 networkx 的绘图功能 pos = nx.spring_layout(nx_graph) nx.draw(nx_graph, pos, with_labels=True, arrows=True) plt.show() ``` --- ## ✅ 示例完整修复代码 ```python # 假设 G_nx 是 causaldag 的 GeneralGraph 对象 existing_nodes = set(G_nx.nodes) for node in range(nodes): if node not in existing_nodes: G_nx.add_node(node) ``` 或者如果你想用 networkx 绘图: ```python import networkx as nx from causaldag.utils.networkx_utils import general_graph_to_nx nx_graph = general_graph_to_nx(G_nx) # 添加缺失的节点 existing_nodes = set(nx_graph.nodes) for node in range(nodes): if node not in existing_nodes: nx_graph.add_node(node) # 绘图 plt.figure(figsize=(8, 6)) pos = nx.spring_layout(nx_graph) nx.draw(nx_graph, pos, with_labels=True, node_size=800, node_color='lightblue', font_size=12, arrows=True) plt.title("Causal Graph") plt.show() ``` --- ## ✅ 总结 | 问题 | 原因 | 解决方法 | |------|------|----------| | `TypeError: not iterable` | `G_nx` 是 `GeneralGraph` 类型,不能用 `in` | 使用 `G_nx.nodes` 获取节点列表 | | 添加节点失败 | 图中可能没有节点 | 提前检查并添加节点 | | 可视化失败 | `GeneralGraph` 不支持直接绘图 | 转换为 `networkx` 图再绘图 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值