题目

What does our future hold? How will the universe meet its end? Over the whole human history, controversy on whether the universe ends in fire or ice has never come to an end. But now, with the second law of thermodynamics, maybe we can conclude that the universe ends in not a giant explosion, but a whimper.

After 10^377 years, this universe is approaching its end. Everything goes together, becoming a black hole, and nothing is left except some dying black dwarf stars which are the only places for possibly remaining civilizations to feed on. Now you are the God and only Sylvie is lingering on with her last breath of life in this universe created by you. With mercy, you are affected by her strong will to survive, so you decide, if she solves the problem you present her, you will ship her to the next universe you create where there are still colours.

So you whispered by her ear: ‘Every black dwarf star has energy, and a string of letters with it. If you live long enough to collect the largest amount of energy from those black dwarf stars, keeping on collecting strings of letters of each star you pass, combining them in turn along the journey, you will find the encoded answer therein. Decode it by finding the sequence of letters with the most front alphabetic order among permutations of the original without changing circular order of its letters. For example, if you pass three stars with string “b”, “cd” and “a” respectively, and combines them to get “bcda”, you can then decode it and get the answer “abcd”.’

Sylvie dies as soon as her energy runs out, and when arriving at a star, the road carrying her there takes certain amount of her energy and disappears, but she collects all the energy of that star which is, sadly, strictly nonrenewable.

There are* N* black dwarf stars and M directed roads between them in the universe and Sylvie is now on star S1 with roads leading to other stars and the energy costs along these roads are given. Every time Sylvie arrives at a planet, the string of the planet is collected again.

Because of the incredible deficiency of energy in the dying universe, even one spark of thought will take billions of years within which Sylvie will die for sure together with the universe. So Sylvie has to get the right answer quickly.

Since you are curious and almighty, you think of the answer immediately after having invented the problem. And in the meanwhile, the idea that putting your wisdom down in form of an ancient language invented by human beings for their computers has come to your mind for fun. Try doing it now!

Input Specification:

The first line are two integers N and M (N>0,M>0,N<=20 M<=300) and the second line, N integers, of which the ith integer describes the energy of the ith black dwarf star. M lines with three integers x,y,e are following, meaning star Sx has one directed road leading to star Sy costing e energy on the road. Finally there are N lines, of which each is a string of letters – the string can be found on the ith star (at the beginning, Sylvie has the energy of the first black dwarf star).

Output Specification:

Altogether two lines, of which the first line is the string of letters you have decoded, while the second, the amount of energy Sylvie collected. There is only one path to collect the largest amount of energy.

Sample Input:

4 5
10 10 10 20
1 2 3
1 3 5
2 4 10
3 4 5
1 4 1
b
f
cd
a

Sample Output:

abcd
30

基本思路

这题可以分为两个子问题:
第一个问题用求一条能获得最大能量的最优路径,跟普通的图的深搜问题不大一样,这里的顶点只要能量足够可以重复去访问,用回溯法解决。
第二个问题是循环字符串的最小表示法(不知道该算法的可以先了解一下),可以套用固定模板。

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//一个黑矮星对应一个结点
struct Star {int power;string letters;bool accessed;
};
Star stars[21];
int cost[21][21];
int n, m;//结点数,边数
int maxEnergyGot;//能获得的最大能量
vector<int> maxSeq;//能获得最大能量的路径void dfs(int start, int energyGot, vector<int> v) {for (int i = 1; i <= n; i++) {//如果顶点start到顶点i有路径 并且 携带的能量足够走这条路路径if (cost[start][i] && energyGot >= cost[start][i]) {//bool changed = false;//判断顶点i是不是在这个dfs中被访问的if (!stars[i].accessed) {//如果顶点i没有被访问过changed = true;stars[i].accessed = true;energyGot += stars[i].power - cost[start][i];} else{//如果顶点i被访问过energyGot += -cost[start][i];}v.push_back(i);//如果临时能量大于最大能量,更新最大能量和最优路径if (energyGot > maxEnergyGot) {maxEnergyGot = energyGot;maxSeq.assign(v.begin(), v.end());//数组拷贝}//对顶点i、临时能量energyGot、临时路径v进行dfsdfs(i, energyGot, v);//v.pop_back();if (changed) {stars[i].accessed = false;energyGot -= stars[i].power - cost[start][i];}else{energyGot -= -cost[start][i];}}}
}int solve(string s){int i=0,j=1;string t=s+s;while (i<s.length() && j<s.length()){int k=0;while(k<s.length() &&t[i+k]==t[j+k]) k++;if(k==s.length()) break;if(t[i+k]>t[j+k]) i=i+k+1;else j=j+k+1;if(i==j) i++;}return  min(i,j);
}int main() {//读入cin >> n >> m;for (int i = 1; i <= n; i++)cin >> stars[i].power;for (int i = 1; i <= m; i++) {int a, b, e;cin >> a >> b >> e;cost[a][b] = e;}for (int i = 1; i <= n; i++)cin >> stars[i].letters;//把起点的能量赋值给最大能量,把起点放入临时路径v,并把临时路径v拷贝给最优路径maxSeqmaxEnergyGot = stars[1].power;vector<int> v;//临时路径vv.push_back(1);maxSeq.assign(v.begin(), v.end());//从起点、起点的能量、临时路径v开始dfsdfs(1, stars[1].power, v);//取出最优路径上每个顶点所携带的字符串letters,把他们拼接成字符串res//最小字典序的循环同构串的求解string res;for(auto i:maxSeq){res+=stars[i].letters;}int pos= solve(res);//输出for(int i=pos;i<res.length();i++)cout<<res[i];for(int i=0;i<pos;i++)cout<<res[i];cout<<endl<<maxEnergyGot<<endl;return 0;
}

7-7 The Whimper of Universe (30 分)相关推荐

  1. 1068 Find More Coins (30分)

    文章目录 1 题目 2 解析 2.1 题意 2.2 思路 3 参考代码 1 题目 1068 Find More Coins (30分) Eva loves to collect coins from ...

  2. 3分和30分文章差距在哪里?

    好的分析和可视化,可以提供大量的信息,同时兼顾简洁优雅. 今天我们抛开实验设计.方法和工作量等因素,仅从文章最吸引人的图片来讨论3分和30分(顶级)文章差距在哪里? 以2017年8月25日发表在Sci ...

  3. 微生物组:3分和30分文章差距在哪里?

    好的分析和可视化,可以提供大量的信息,同时兼顾简洁优雅. 今天我们抛开实验设计.方法和工作量等因素,仅从文章最吸引人的图片来讨论3分和30分(顶级)文章差距在哪里? 以2017年8月25日发表在Sci ...

  4. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  5. PAT甲级1147 Heaps (30 分):[C++题解]堆、树的遍历、dfs、完全二叉树建树

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:给定完全二叉树,判断是否是堆,需要区分大根堆,小根堆.后面是输出后序遍历. AC代码 #include<bits/stdc++. ...

  6. PAT甲级1076 Forwards on Weibo (30 分) :[C++题解]图论、bfs

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: BFS如何搜前k层?统计前k层的点数. ac代码 #include<bits/stdc++.h> using names ...

  7. PAT甲级1072 Gas Station (30 分):[C++题解]dijkstra算法、最短路

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 所有的dist[ ]都≤Ds:最小的dist[ ]最大; dist[ ] 总和最大. 由于加油站是字符,为了简单起见,将m个加油站编 ...

  8. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  9. PAT甲级1155 Heap Paths (30 分):[C++题解]堆、堆的遍历、树的遍历、dfs输出路径、完全二叉树建树

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 堆首先是完全二叉树,所以先建完全二叉树,由于给定的是层序遍历的数据,所以直接用数组即可,注意数组下标从1开始,这样便满足结点u和左儿 ...

最新文章

  1. python 中cookie_详解Python中的Cookie模块使用
  2. (转)简单代码生成器原理剖析(二)
  3. [转]caffe中solver.prototxt参数说明
  4. Java Web应用小案例:查询城市天气信息
  5. 机器人学 —— 机器人感知(Kalman Filter)
  6. mybatis中#{}和${}
  7. 深入理解 Linux 的 RCU 机制
  8. 【目标识别】SIFT算法理论部分
  9. 三、Oracle 查询+where条件
  10. 首个万人远程培训项目顺利启动
  11. 深度学习CNN系列笔记
  12. 图像处理黑科技—破解文档识别难题(PS检测、弯曲拉平、切边切片、摩尔纹)
  13. 【鸿蒙学院】鸿蒙App开发直播学员提问与回答
  14. 五百亿!又一大型平台崩了!85后女老板跑路!
  15. 基于hi3531、ffmpeg、x264的h264压缩
  16. 指针学习中二维数组解引用问题
  17. IBM创始人 托马斯·约翰·沃森父子
  18. Android:多分辨率适配
  19. GPS七参数计算工具
  20. 服务器虚拟内存 关闭,关机 清除 Windows 10 (虚拟内存) - Windows security | Microsoft Docs...

热门文章

  1. Cache replacement policies(缓存替换策略)/ LRU 和 LFU等算法
  2. 抖音视频二创闷声发财
  3. 补齐AI人才短板!百度飞桨师资培训高校行走进天津大学
  4. 软件测试基础面试常问问题(三)
  5. 自定义事件 (子组件事件激发父组件里子组件模块的事件)
  6. 掌握PS制作,实时预览你的精彩作品
  7. 运动想象| EEG信号、共空间模式算法(CSP)
  8. html5QQ浏览器页面引导模板,手机QQ浏览器 策略打造HTML5开放平台
  9. 华为od统一考试B卷【用连续自然数之和来表达整数】C++ 实现
  10. 守护线程(Daemon)、钩子线程(Hook)简述