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 ...
随机推荐
- 谈谈Command对象与数据检索
摘要 到目前为止,我相信大家对于ADO.NET如何与外部数据源建立连接以及如何提高连接性能等相关知识已经牢固于心了.连接对象作为ADO.NET的主力先锋,为用户与数据库交互搭建了扎实的桥梁.它的一生是 ...
- web自动化测试 Selenium2 Java自动化测试实战9_3
driver.findElement(By.id("idInput")).sendKeys("哈哈"); driver.findElement(By.id(&q ...
- 00096_Properties类
1.Properties类介绍 (1)Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串: (2)特点 Hasht ...
- COGS——C2098. Asm.Def的病毒
http://www.cogs.pro/cogs/problem/problem.php?pid=2098 ★☆ 输入文件:asm_virus.in 输出文件:asm_virus.out ...
- 关于Clipboard和GlobalAlloc函数的关系
一句话:为了满足进程间通信,使用了clipboard的方法,clipboard是系统提供的一段任何进程都可以访问的公共内存块,malloc 和new分配的动态内存块是在进程的私有地址空间分配的,所以必 ...
- [Vue] Load components when needed with Vue async components
In large applications, dividing the application into smaller chunks is often times necessary. In thi ...
- linux终端下一些“风骚”的按键操作及Linux终端命令
linux终端下一些"风骚"的按键操作 <backspace> 删除 <ctrl-l> 清空屏幕, 相当于clear tab ...
- Nginx 虚拟主机及正向代理设置
添加虚拟主机 # vim /usr/local/nginx-1.9.0/conf/vhost/proxy.conf server { resolver 8.8.8.8; listen ; locat ...
- NSArray NSDictionary一些用法
//从字符串分割到数组- componentsSeparatedByString: NSString *str = [NSString alloc] initWithString:@"a,b ...
- ocx 中使用CImage和CComPtr
#include <atlimage.h> using namespace ATL;