1053. Path of Equal Weight (30)

本文介绍了一个基于C++实现的算法,该算法通过遍历树形结构来找出所有权重之和等于给定值S的路径,并将这些路径上的节点权重按特定格式输出。文章详细展示了如何构建节点结构、遍历树形结构以及输出结果的具体步骤。

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

从叶子节点开始找比较方便。另外输出内容排序可以把输出串放在string中排序再输出比较方便

#define  _CRT_SECURE_NO_WARNINGS 

#include<string>
#include<string.h>
#include<vector>
#include<map>
#include<stack>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<stdio.h> 
using namespace std; 
bool cmp(string a, string b)
{
return a > b;
}
struct Route
{
vector<int> r;
int wei;
};
struct Node
{
int parent;
vector<Route> route;
bool leaf;
Node()
{
parent = -1;
leaf = true;
}


};
int mm[100000] = {0};
int main()

int N, M, S;
cin >> N >> M >> S;
int *weight = new int[N];
for (int i = 0; i < N; i++)
scanf("%d",&weight[i]);
Node *node = new Node[N];
for (int i = 0; i < M; i++)
{
int key, size;
scanf("%d %d", &key, &size);
node[key].leaf = false;
for (int j = 0; j < size; j++)
{
int tmp;
scanf("%d", &tmp);
//node[key].child.push_back(tmp);
node[tmp].parent = key;

}
for (int i = 0; i < N; i++)
{
if (node[i].leaf)
{
Route r;
r.r.push_back(i);
r.wei = weight[i]; 


int par = node[i].parent;
if (par==-1)
node[i].route.push_back(r);
while (par != -1)
{
r.r.push_back(par);
r.wei += weight[par];
if (node[par].parent==-1)
node[par].route.push_back(r);
par = node[par].parent;
}
}
}
vector<string> out;
for (int j = 0; j < node[0].route.size(); j++)
{
if (node[0].route[j].wei == S)
{
string sTmp;
vector<int> vR = node[0].route[j].r;
for (int k = vR.size() - 1; k >= 0; k--)
{
char c_s[4];
sprintf(c_s, "%03d", weight[vR[k]]);
sTmp += c_s;
}
out.push_back(sTmp);
}
}
sort(out.begin(), out .end(),cmp);
for (int i = 0; i < out.size(); i++)
{
string sT = out[i];
printf("%d", atoi(sT.substr(0, 3).c_str()));
for (int j = 3; j < sT.size(); j += 3)
{
int i = atoi(sT.substr(j,3).c_str());
printf(" %d",i);
}
cout << endl;
}
return 0;
}
这是对单个文件进行预测“import os import json import torch from PIL import Image from torchvision import transforms import matplotlib.pyplot as plt from model import convnext_tiny as create_model def main(): device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print(f"using {device} device.") num_classes = 5 img_size = 224 data_transform = transforms.Compose( [transforms.Resize(int(img_size * 1.14)), transforms.CenterCrop(img_size), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) # load image img_path = "../tulip.jpg" assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path) img = Image.open(img_path) plt.imshow(img) # [N, C, H, W] img = data_transform(img) # expand batch dimension img = torch.unsqueeze(img, dim=0) # read class_indict json_path = './class_indices.json' assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path) with open(json_path, "r") as f: class_indict = json.load(f) # create model model = create_model(num_classes=num_classes).to(device) # load model weights model_weight_path = "./weights/best_model.pth" model.load_state_dict(torch.load(model_weight_path, map_location=device)) model.eval() with torch.no_grad(): # predict class output = torch.squeeze(model(img.to(device))).cpu() predict = torch.softmax(output, dim=0) predict_cla = torch.argmax(predict).numpy() print_res = "class: {} prob: {:.3}".format(class_indict[str(predict_cla)], predict[predict_cla].numpy()) plt.title(print_res) for i in range(len(predict)): print("class: {:10} prob: {:.3}".format(class_indict[str(i)], predict[i].numpy())) plt.show() if name == 'main': main()”,改为对指定文件夹下的左右文件进行预测,并绘制混淆矩阵,
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值