【BZOJ】1975: [Sdoi2010]魔法猪学院
2015-11-22 13:30
281 查看
题意
\(n(2 \le n \le 5000)\)个点,找尽量多的不同\(1\)到\(n\)的路径,每一次的花费就是路径的全值和,要求在费用不超过\(E\)的情况下路径最多。分析
裸的最段路。题解
A*算法即可。#include <bits/stdc++.h> using namespace std; typedef double lf; const int N=5005, M=200005; lf d , En; typedef pair<lf, int> pr; #define mkpr(x, y) make_pair<lf, int> (x, y) priority_queue<pr, vector<pr>, greater<pr> >q; struct Gr { struct E { int next, to; lf w; }e[M]; int n, ihead , cnt; void add(int x, int y, lf w) { e[++cnt]=(E){ihead[x], y, w}; ihead[x]=cnt; } void dij() { static bool vis ; memset(vis, 0, sizeof(bool)*(n+1)); for(int i=1; i<=n; ++i) { d[i]=1e150; } d =0; q.push(mkpr((lf)0, n)); while(q.size()) { int x=q.top().second; q.pop(); if(vis[x]) { continue; } vis[x]=1; for(int i=ihead[x]; i; i=e[i].next) { int y=e[i].to; if(d[y]>d[x]+e[i].w) { d[y]=d[x]+e[i].w; q.push(mkpr(d[y], y)); } } } } int getans() { int ans=0; q.push(mkpr(d[1], 1)); while(En>0 && q.size()) { int x=q.top().second; lf g=q.top().first-d[x]; q.pop(); if(x==n) { if(En>=g) { En-=g; ++ans; continue; } else { break; } } for(int i=ihead[x]; i; i=e[i].next) { int y=e[i].to; lf w=e[i].w; // 有精度误差啊,不能乱减枝啊 q.push(mkpr(g+w+d[y], y)); } } return ans; } }g, G; int main() { int n, m; scanf("%d%d%lf", &n, &m, &En); g.n=G.n=n; for(int i=1; i<=m; ++i) { int x, y; lf e; scanf("%d%d%lf", &x, &y, &e); g.add(x, y, e); G.add(y, x, e); } G.dij(); printf("%d\n", g.getans()); return 0; }
相关文章推荐
- 搭建nginx的https出现的64位平台架构问题
- 架构那些事3--架构演进
- printf
- Ubuntu14.04安装JDK与配置环境变量
- php不解析的排查步骤
- Android学习手记(5) 基本UI布局
- core data的复习与使用
- scanf
- Java Web 数据安全-面向Tomcat Server的HTTPS安全套接层配置
- 【BZOJ】1535: [POI2005]Sza-Template
- 机器学习实战笔记(1)机器学习基础
- 华为OJ中级题-查找两个字符串a,b中的最长公共子串
- linux 775和777权限有什么区别
- [LeetCode]Jump Game II
- gdb逆向调试
- ubuntu14.04完全卸载mysql
- mac 在 finder 当前 路径下 打开 terminal 的办法
- 架构的那些事2--安卓的奋进
- linux su和sudo命令的区别
- 数据类型输出