POJ1724 ROADS
来源:互联网 发布:淘宝客服培训ppt模板 编辑:程序博客网 时间:2024/05/17 05:04
这个题目是一个比较简单dfs,用到的技巧并不多,只是常用的剪枝。两处剪枝即可:
1,当价钱超出K时;
2,当前的道路长度大于已经得到的道路长度时;
这个题目还有一点值的注意的是,不要用vector存图,这样容易TLE。改用邻接表就好。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int MAX = 110;const int INF = 10000000;struct Edge{ int from, to, dist, cost;}edges[10010];int first[10010],next[10010],pro;void AddEdge(int from, int to, int dist, int cost){ edges[pro].from = from; edges[pro].to = to; edges[pro].dist = dist; edges[pro].cost = cost; next[pro] = first[from]; first[from] = pro++;}int vis[MAX];int n,m,mindist;void dfs(int s, int restcost, int nowdist){ // printf("s = %d nowcost = %d nowdist = %d\n",s,nowcost, nowdist); if (nowdist > mindist) return; if (s == n && restcost >= 0 && nowdist < mindist){ mindist = nowdist; return; } for (int i = first[s]; i != -1; i = next[i]){ Edge e = edges[i]; if (!vis[e.to] && restcost >= e.cost){ vis[e.to] = 1; dfs(e.to, restcost-e.cost, nowdist+e.dist); vis[e.to] = 0; } } return;}int main(){ int K; while (scanf("%d%d%d",&K,&n,&m) != EOF){ int x, y, z, w; pro = 0; memset(next, -1, sizeof(next)); memset(first, -1, sizeof(first)); memset(vis, 0,sizeof(vis)); for (int i = 0; i<m; i++){ scanf("%d%d%d%d",&x,&y,&z,&w); AddEdge(x,y,z,w); } mindist = INF; dfs(1,K,0); if (mindist < INF) printf("%d\n",mindist); else printf("-1\n"); } return 0;}
0 0
- poj1724 ROADS
- poj1724 - ROADS
- ROADS poj1724
- POJ1724--ROADS
- POJ1724 ROADS
- POJ1724-ROADS
- poj1724 ROADS
- poj1724 ROADS
- POJ1724 ROADS
- poj1724——ROADS
- poj1724 ROADS (spfa + A*)
- poj1724 ROADS 解题报告
- poj1724 roads(dfs)
- poj1724——ROADS
- POJ1724 ROADS最短路问题
- poj1724 openjudge1724 ROADS 解题记录
- 【POJ1724】ROADS 某邪恶最短路
- poj1724--ROADS(最短路变形)
- YUM__Skip broken
- c++与java通讯之jni
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- Android AsyncTask的使用
- C++中析构函数的作用
- POJ1724 ROADS
- Linux中设置服务自启动的三种方式
- 彻底理解position与anchorPoint - 记忆丶腐朽年华
- mac svn命令 linux同样适用
- 详解Android中AsyncTask的使用加入了cancle和progerssbar
- 设备驱动基础1:设备模型之总线,驱动,设备
- 使用xmanager 从win7链接Centos
- Linux TOP命令 按内存占用排序和按CPU占用排序
- web前端面试题