POJ 1724 ROADS
来源:互联网 发布:编程原本 pdf 下载 编辑:程序博客网 时间:2024/05/29 18:10
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;struct node{ int v,l,t;};const int maxn=100+10;const int maxl=10000+10;vector<node> p[maxn];int k,n,r;int cost,bestcost;int vis[maxn],f[maxn][maxl];void dfs(int u,int k){ if(k<0) return ; if(u==n){ bestcost=min(bestcost,cost); return ; } int d=(int)p[u].size(); for(int i=0;i<d;i++) { int v=p[u][i].v,l=p[u][i].l,t=p[u][i].t; if(!vis[v])//vis标记数组很重要,防止重复扩展结点导致错误解 { if(cost+l>f[v][k-t]||cost+l>bestcost) continue;//这个剪枝十分重要,没有这个剪枝过不了,剪枝的意思是如果到下个点所走的长度比相同点,相同金钱的还要小,那就不用扩展了,肯定是上次扩展的更优,下一个剪枝显而易见,如果现在已经走的路径比过去已经成功的最优路径还要长的话也停止扩展; f[v][k-t]=cost+l; cost+=l; vis[v]=1; dfs(v,k-t); vis[v]=0; cost-=l; } }}int main(){ int u; node q; scanf("%d%d%d",&k,&n,&r); memset(f,1,sizeof(f)); for(int i=1;i<=r;i++) { scanf("%d%d%d%d",&u,&q.v,&q.l,&q.t); p[u].push_back(q); } cost=0; bestcost=(1<<30); dfs(1,k); if(bestcost==(1<<30)) printf("-1\n"); else printf("%d\n",bestcost); return 0;}
0 0
- POJ 1724 ROADS
- POJ 1724 ROADS
- poj 1724 ROADS
- Poj 1724 ROADS
- POJ-1724-ROADS
- poj 1724 ROADS
- POJ 1724 ROADS
- POJ-1724-ROADS
- POJ 1724 ROADS (bfs)
- poj 1724 ROADS
- POJ 1724 ROADS
- POJ 1724 ROADS
- POJ 1724 ROADS
- POJ 1724 ROADS
- POJ 1724--ROADS
- poj 1724 ROADS
- poj 1724 ROADS
- POJ-1724 ROADS
- 《React-Native系列》5、RN实现弹出选择界面与动画效果
- 杭电 Problem 2008 分拆素数和 【打表】
- java中类名.class, class.forName(), getClass()区别
- 《剑指offer》第39题:平衡二叉树
- 最长回文串
- POJ 1724 ROADS
- 一个makefile使用实例
- hdoj 2098 分拆素数和 【素数打表】
- Java烧脑驴游(十四)--流(Stream)、文件(File)和IO
- 无线网覆盖
- 一个bat使用实例
- uboot 移植
- shell脚本 - 检测mysql是否可用
- NYOJ-35-表达式求值(栈)