UVA 10917 Walk Through the Forest 最短路 + DP
求出最短路后,如果d[A]<d[B] 则B可以走向A
是个DAG。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) {return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) {return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #elsefreopen("d:\\in1.txt","r",stdin);freopen("d:\\out1.txt","w",stdout); #endif } int getch() {int ch;while((ch=getchar())!=EOF) {if(ch!=' '&&ch!='\n')return ch;}return EOF; }struct Edge {int from,to,dist; }; struct HeapNode {int d,u;bool operator < (const HeapNode &ant ) const{return d>ant.d;} };const int maxn=1005; int n; vector<int> g[maxn]; vector<Edge> edge;void InitGraph() {for(int i=1;i<=n;i++)g[i].clear();edge.clear(); }void AddEdge(int u,int v,int w) {Edge e=(Edge){u,v,w};edge.push_back(e);g[u].push_back(edge.size()-1); }int done[maxn]; int d[maxn];void Dijksta(int s) {priority_queue<HeapNode> q;q.push((HeapNode){0,s});for(int i=1;i<=n;i++)d[i]=INF;d[s]=0;memset(done,0,sizeof(done));while(!q.empty()){HeapNode x=q.top();q.pop();if(done[x.u])continue;done[x.u]=1;for(int i=0;i<g[x.u].size();i++){Edge &e=edge[g[x.u][i]];if(d[e.to]>d[x.u]+e.dist){d[e.to]=d[x.u]+e.dist;q.push((HeapNode){d[e.to],e.to});}}} }int dp[maxn]; int f(int u) {if(u==2)return 1;if(dp[u]>=0)return dp[u];dp[u]=0;for(int i=0;i<g[u].size();i++){int v=edge[g[u][i]].to;if(d[v]<d[u])dp[u]+=f(v);}return dp[u]; } int DP() {memset(dp,-1,sizeof(dp));return f(1); }int main() {while(scanf("%d",&n)!=EOF&&n){InitGraph();int m;scanf("%d",&m);for(int i=1;i<=m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);AddEdge(u,v,w);AddEdge(v,u,w);}Dijksta(2);int res=DP();printf("%d\n",res);}return 0; }
View Code
转载于:https://www.cnblogs.com/BMan/p/3632899.html
UVA 10917 Walk Through the Forest 最短路 + DP相关推荐
- UVA 10917 Walk Through the Forest
最短路+DP Walk Through the Forest Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- A Walk Through the Forest HDU - 1142(dijkstra+动态规划)
题目大意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路, 且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的 一条路.问满足这样的路径条数 有多少... 解题思路 ...
- P1772 [ZJOI2006]物流运输 最短路+DP
思路:最短路+DP 提交:1次 题解: $f[i]$表示到第$i$天的最小代价,我们可以预先处理出$i,j$两天之间(包括$i,j$)都可通行的最短路的代价记做$s[i][j]$,然后有$f[i]=m ...
- 【HYSBZ - 2763 】飞行路线 (分层图最短路,最短路dp)
题干: Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价 ...
- 【牛客 - 370B】Rinne Loves Graph(分层图最短路 或 最短路dp)
题干: Island 发生了一场暴乱!现在 Rinne 要和 Setsuna 立马到地上世界去. 众所周知:Island 是有一些奇怪的城镇和道路构成的(题目需要,游戏党勿喷),有些城镇之间用双向道路 ...
- A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 题解: 看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A 和 B 是相连的,当前 ...
- UVA10917 Walk Through the Forest
题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 ...
- Uva 11374 - Airport Express(枚举+最短路)
题目链接 https://vjudge.net/problem/UVA-11374 [题意] 市民从市区去机场要走机场快线,机场快线分为经济线和商业线两种,你只有一张商业线车票,只能坐一站商业线,其它 ...
- HDU 1142 A Walk Through the Forest dijkstra + DFS
http://acm.hdu.edu.cn/showproblem.php?pid=1142 题意: Jimmy在位置 1 ,每天晚上要回位置2(家),计算1到2的最短距离,Jimmy要先去一个地方然 ...
- A Walk Through the Forest dijkstra(邻接矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=1142 dijkstra(邻接矩阵) 1 #include <cstdio> 2 #include & ...
最新文章
- .NET读写Excel工具Spire.Xls使用(1)入门介绍
- 防止多次提交的几个比较
- 通过物理映射往虚拟机中传输数据
- bootstrap外不引用连接_网络编程Netty IoT百万长连接优化,万字长文精讲
- 音量已经调到100%,如何再调整
- layoutSubviews调用
- 2017.9.13 序列统计 思考记录
- Win7下部署Lepus企业级MySQL数据库监控
- 基于html5的旅游交流系统,基于HTML5的旅游移动导览系统的研究与实现
- 17.1.1.3 Creating a User for Replication 创建一个用于用于复制:
- KORG Opsix Native Mac - 音频数字合成器
- Oracle导入元数据,eova oracle 导入元数据报错
- 北京内推 | 微软亚洲研究院自然语言计算组招聘NLP研究实习生
- 2023 抖音表情包小程序变现项目 详细玩法视频课程
- websocket协议与实现原理
- pg 快速造1000w测试数据
- 18天精读掌握《费曼物理学讲义卷一》 第7天 2019.6.20
- 数据结构-二路归并及归并排序
- 叉积 微分 恒等式_最美的公式:你也能懂的麦克斯韦方程组(微分篇)(下)...
- 第2章第6节:艺术字体的下载和安装和嵌入 [PowerPoint精美幻灯片实战教程]