HDU 1224 Free DIY Tour - 最短路
题目大意:
一个有向图(n + 1相当于1),每个点有一个权值(可以认为1和n+1权值为0),求从1走到n+1(相当于走回1)的最大路径权值和是多少,输出方案。
题目分析:
最短路问题,输出方案只需在dijkstra更新时记录from数组,完成后倒推即可。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace IO{
inline ll read(){
ll i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 105, OO = 0x3f3f3f3f;
int n, dis[N], T, m, val[N], from[N], k;
bool gra[N][N];
typedef pair<int, int> P;
priority_queue<P> que;
inline void solve(){
memset(dis, -OO, sizeof dis), dis[1] = 0;
que.push(P(0, 1));
while(!que.empty()){
P t = que.top(); que.pop();
for(int i = t.second + 1; i <= n + 1; i++){
if(i == t.second || !gra[t.second][i]) continue;
// cout<<t.second<<"->>"<<i<<endl;
// cout<<dis[3]<<" "<<dis[1] + val[3]<<endl;
if(dis[i] < t.first + val[i]){
dis[i] = t.first + val[i];
from[i] = t.second;
que.push(P(dis[i], i));
}
}
}
vector<int> ans; ans.push_back(n + 1);
int now = n + 1; while(from[now]) ans.push_back(from[now]), now = from[now];
printf("CASE %d#\npoints : %d\ncircuit : ", ++k, dis[n + 1]);
for(int i = ans.size() - 1; i > 0; i--) printf("%d->", ans[i]); wr(1);
printf("\n");
}
int main(){
freopen("h.in", "r", stdin);
T = read();
while(T--){
memset(gra, 0, sizeof gra), memset(from, 0, sizeof from), memset(val, 0, sizeof val);
n = read(); for(int i = 1; i <= n; i++) val[i] = read();
m = read(); for(int i = 1; i <= m; i++){
int x = read(), y = read();
if(x > y) swap(x, y);
gra[x][y] = true;
}
solve();
if(T) printf("\n");
}
return 0;
}
HDU 1224 Free DIY Tour - 最短路的更多相关文章
- HDU 1224 Free DIY Tour(spfa求最长路+路径输出)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 1224 Free DIY Tour(最长的公路/dp)
http://acm.hdu.edu.cn/showproblem.php? pid=1224 基础的求最长路以及记录路径. 感觉dijstra不及spfa好用,wa了两次. #include < ...
- HDU 1224 Free DIY Tour
题意:给出每个城市interesting的值,和城市之间的飞行路线,求一条闭合路线(从原点出发又回到原点) 使得路线上的interesting的值之和最大 因为要输出路径,所以用pre数组来保存前驱 ...
- HDU ACM 1224 Free DIY Tour (SPFA)
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 【HDOJ】1224 Free DIY Tour
DP. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm ...
- hdu 1224(动态规划 DAG上的最长路)
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 动态规划:HDU1224-Free DIY Tour
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- A - Free DIY Tour HDU - 1224
题目大意:每一个城市都有一定的魅力值,然后有一个有向图,根据这个有向图从1到n+1所获得的魅力的最大值,并输出路径(要求只能从编号娇小的城市到编号较大的城市). 题解:很容易想到最短路+路径纪录.但是 ...
- hdu Free DIY Tour
http://acm.hdu.edu.cn/showproblem.php?pid=1224 #include <cstdio> #include <cstring> #inc ...
随机推荐
- jedate-开始使用一款好用的时间插件
jeDate日期控件 -(原生JS版)jeDate V6.5.0 是一款原生JS开发的 不依赖任何第三方库 大众化的日期控件,包含 多语言.设定年月(YYYY-MM).日期范围限制.开始日期设定.自定 ...
- C# C++ 字符串传递
C# C++ 字符串传递 标签: c#c++bytestring测试c 2012-06-14 17:425707人阅读评论(3)收藏举报 分类: C#(11) 作者同类文章X C++(112) 作 ...
- 【例题 7-3 UVA - 10976】Fractions Again?!
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] x>=y => \(\frac{1}{x}<=\frac{1}{y}\) => \(\frac{1}{x}= ...
- 几个移动web app开发框架
几个移动web app开发框架 一.总结 1.有amaze ui,有app.js(登录注册界面用到的) 二.几个移动web app开发框架 jQuery Mobile jQuery Mobile框架 ...
- 什么是MVC,什么是WCF
在C#中总会遇到这几个概念,网上搜了一下,做一下总结和比较,东拼西凑,如有雷同,纯属直接拷贝,人懒,但无意侵权. 1.什么是MVC MVC是三个单词的首字母缩写,它们是Model(模型).View(视 ...
- css使文本保留多个空格
css属性: white-space: pre-wrap
- C# is 和 as的用法
try { if (sender is Button) { Button dd ...
- 30分钟学会如何使用Shiro(转)
本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...
- 20160206.CCPP体系具体解释(0016天)
代码片段(01):.指针.c+02.间接赋值.c 内容概要:内存 ///01.指针 #include <stdio.h> #include <stdlib.h> //01.取地 ...
- 使用MongoDb连接数据库服务器
链接MongoDb数据库服务器的字符串格式: mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN] ...