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 ...
随机推荐
- c#中反射的用法(即如何根据字符找到已定义的变量)
2013-07-20 08:06 720人阅读 评论(0) 收藏 举报 分类: C#(9) 作者同类文章 X 版权声明:本文为博主原创文章,未经博主允许不得转载. 常常羡慕javascript中, ...
- C# 数据通信
json asmxwcfwebRequestwebClient 串口 socket
- java和 javaw 以及 javaws的区别
http://blog.csdn.net/topwqp/article/details/8595936
- 洛谷—— P1091 合唱队形
https://www.luogu.org/problem/show?pid=1091#sub || http://codevs.cn/problem/1058/ 题目描述 N位同学站成一排,音乐 ...
- __block typeof的说明
1. block不是Object对象,所以对retain无效,要想保留block生命周期,最好通过copy来实现,当然copy后,要记得release. 2.一般被block的应用的对象,retain ...
- js进阶 12-14 jquery的事件触发函数是哪两个
js进阶 12-14 jquery的事件触发函数是哪两个 一.总结 一句话总结:trigger和triggerHandler 1.trigger传额外参数时候的注意事项是什么? 注意样例中是三个参数 ...
- context.getSystemService的简单说明
在android开发过程中这种方法一定不会陌生,比方我们在获取WindowManager和LayoutInflater对象的时候就须要我们调用这种方法.这种方法在context这个抽象类的一个抽象方法 ...
- 一、Github博客搭建之jekyll安装
注意:以下步骤是FQ后操作的,需要了解FQ的可以移步 -> 枫叶主机 一.安装jekyll需要Ruby-2.1.0以上版本,本人是mac pro系统版本10.12.5(macOS Sierra) ...
- WPF应用程序启动的问题(自定义Main函数启动)
问题引入: 一般WPF创建之后可以直接运行并不需要编写Main函数指定入口,但是在开发的过程中会遇到一些情况需要自定义Main让WPF从指定的Main函数中进行启动,这样可能会更好控制一点.但是我们再 ...
- PBOC
http://blog.sina.com.cn/s/blog_64cc82620100rcgu.html 最近在做一个基于PBOC电子现金卡的终端应用, 项目还没有完成, 但电子现金部分的处理模块已完 ...