1053. Path of Equal Weight 解析
来源:互联网 发布:如何做淘宝链接 编辑:程序博客网 时间:2024/06/09 17:52
直接DFS回溯就好,到叶子节点判定是否满足条件,满足加入结果。
最后排序就好。
#include <iostream>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <vector>#include <map>#include <queue>#include <stack>#define MAX 105using namespace std;struct Node{int weight;vector <int> next;};Node t[MAX];int n,m,s;vector <vector<int>> ans;vector <int> patch;void DFS(int root){//跳进去patch.push_back(t[root].weight);if(t[root].next.size()==0){//到了叶子节点int sum = 0;for(int i = 0 ; i< patch.size();i++)sum += patch[i];if(sum == s)ans.push_back(patch);elsereturn;}for(int i = 0 ; i < t[root].next.size() ;i++){DFS(t[root].next[i]);patch.pop_back();//跳出来}}bool cmp(vector <int> v1,vector <int> v2){return v1 > v2;}int main(){scanf("%d%d%d",&n,&m,&s);for(int i = 0 ;i < n ;i++)scanf("%d",&t[i].weight);int no,times;for(int i =0 ; i < m;i++){scanf("%d%d",&no,×);int next;for(int j = 0; j < times;j++){scanf("%d",&next);t[no].next.push_back(next);}}DFS(0);sort(ans.begin(),ans.end(),cmp);for(int i =0 ;i < ans.size();i++){printf("%d",ans[i][0]);for(int j = 1 ; j < ans[i].size() ;j++)printf(" %d",ans[i][j]);printf("\n");}return 0;}
阅读全文
0 0
- 1053. Path of Equal Weight 解析
- 1053. Path of Equal Weight
- 1053. Path of Equal Weight
- 1053. Path of Equal Weight
- 1053. Path of Equal Weight
- 1053.Path of Equal Weight
- 1053. Path of Equal Weight
- 1053. Path of Equal Weight
- 1053. Path of Equal Weight
- Path of Equal Weight
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)-PAT
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight
- PAT 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- c#连接Oracle
- 小程序资料
- 银行管理系统遇到的BUG+结束总结
- A
- Java开发者需要了解哪些常见的开发工具
- 1053. Path of Equal Weight 解析
- C++类的静态变量成员和静态成员函数的使用
- 【算法导论学习笔记】最大子数组问题
- 调整打印机适应套打的边距
- 欧拉函数
- text-align无法实现居中
- C++函数返回引用
- 安装imagej
- Shell 流程控制~09