1053. Path of Equal Weight (30)
来源:互联网 发布:淘宝网唯品会 编辑:程序博客网 时间:2024/05/21 17:49
题目地址:http://www.patest.cn/contests/pat-a-practise/1053
考察排序 和 深度搜索(用递归实现),要按照题目的要求输出,刚开始用了队列,没能按找要求输出
#include <stdio.h>#include <stdlib.h>#include <iostream>#include <vector>#include <math.h>#include <string>#include <string.h>#include <queue>#include <unordered_map>#include <algorithm>#include <sstream>using namespace std;#define N 101int n,m;long long s;struct mydata{ int id; int weight; long long dis; vector<int> children;};mydata v[N];int pre[N];vector<int> ans;bool cmp(int x, int y){ if (v[x].weight > v[y].weight) return true; return false;}void func(int num){ int len = v[num].children.size(); if (len == 0) { if (v[num].dis == s) { ans.push_back(num); } return; } if (len > 1) sort(v[num].children.begin(), v[num].children.end(), cmp); int i; for (i = 0; i < len; i++) { int id = v[num].children[i]; v[id].dis = v[id].weight + v[num].dis; pre[id] = v[num].id; func(id); }}//void func()//{// int i, j;// v[0].dis = v[0].weight;// pre[0] = -1;// queue<int> que;// que.push(v[0].id);//// while (!que.empty())// {// mydata dtnow = v[que.front()];// que.pop();// // int len = dtnow.children.size();//// if (dtnow.dis == s && len == 0)// {// ans.push_back(dtnow.id);// }//// if (len > 1)// sort(dtnow.children.begin(), dtnow.children.end(), cmp);//// for (i = 0; i < len; i++)// {// int id = dtnow.children[i];// v[id].dis =v[id].weight + dtnow.dis;// pre[id] = dtnow.id;// que.push(id);// }// }// // int ansLen = ans.size();// for(i = 0; i < ansLen; i++)// {// int no = ans[i];// vector<int> pp;// pp.clear();// while (no != -1)// {// pp.push_back(v[no].weight);// no = pre[no];// }// // int lenn = pp.size();// printf("%d", pp[lenn-1]);// for (j = lenn -2 ; j >= 0; j--)// {// printf(" %d", pp[j]);// }// printf("\n");// }// printf("\n");//}int main(){ //freopen("in", "r", stdin); int i; scanf("%d%d%ld", &n, &m, &s); for (i = 0; i < n; i++) { scanf("%d", &v[i].weight); v[i].id = i; } int j,id, k, tmp; for (i = 0; i < m; i++) { scanf("%d%d", &id, &k); for (j = 0; j < k; j++) { scanf("%d", &tmp); v[id].children.push_back(tmp); } } pre[0] = -1; v[0].dis = v[0].weight; func(0); int ansLen = ans.size(); for (i = 0; i < ansLen; i++) { int no = ans[i]; vector<int> pp; pp.clear(); while (no != -1) { pp.push_back(v[no].weight); no = pre[no]; } int lenn = pp.size(); printf("%d", pp[lenn - 1]); for (j = lenn - 2; j >= 0; j--) { printf(" %d", pp[j]); } printf("\n"); } //printf(""); return 0;}
0 0
- 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 (30)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- 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)
- 1053. Path of Equal Weight (30)
- 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)
- 1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)
- Android设计模式—策略模式
- ActionBar之我的记录
- 程序员必须掌握的十种算法---二分查找算法
- iOS开发 -- 耦合 ,target/action设计模式
- linux查看目录占用空间
- 1053. Path of Equal Weight (30)
- C#中的JSON封装与解析
- c++ primer plus 第七章-编程题7.13.6《Fill_array,Show_array,Reverse_array》
- gitlab操作命令
- sshd源码下载与编译安装
- BMP位图信息·DIB结构
- git 代码协同之回车问题
- hdu(5402)——Travelling Salesman Problem(模拟题)
- common.io中Tailer类读取文件内容乱码问题解决