poj_3411 Paid Roads(dfs技巧)
来源:互联网 发布:上海万国数据 编辑:程序博客网 时间:2024/05/17 06:06
Description
A network of m roads connects N cities (numbered from 1 toN). There may be more than one road connecting one city with another. Some of the roads are paid. There are two ways to pay for travel on a paid roadi from city ai to city bi:
- in advance, in a city ci (which may or may not be the same asai);
- after the travel, in the city bi.
The payment is Pi in the first case and Ri in the second case.
Write a program to find a minimal-cost route from the city 1 to the city N.
Input
The first line of the input contains the values of N and m. Each of the following m lines describes one road by specifying the values ofai, bi, ci,Pi, Ri (1 ≤ i ≤ m). Adjacent values on the same line are separated by one or more spaces. All values are integers, 1 ≤m, N ≤ 10, 0 ≤ Pi , Ri ≤ 100,Pi ≤ Ri (1 ≤ i ≤ m).
Output
The first and only line of the file must contain the minimal possible cost of a trip from the city 1 to the cityN. If the trip is not possible for any reason, the line must contain the word ‘impossible’.
Sample Input
4 51 2 1 10 102 3 1 30 503 4 3 80 802 1 2 10 101 3 2 10 50
Sample Output
110
从城市a到城市b有若干条路,每条路都需要过路费,可以选择先在城市c交,也可以后到城市b交,费用不同。
求城市1到城市n的最少费用。
题目所构造的图有环,而且有环的方案可能更优,所以直接标记城市是否走过来dfs是不能求出答案的,因为有些城市可能走过不止一次。
那么可以考虑城市最多走过的次数,由于边数为m条,所以最多走过的次数应该是m/2。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 20#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Edge{ int from, to, cc, dist, dist2; Edge(int a, int b, int c, int d, int d2) : from(a), to(b), cc(c), dist(d), dist2(d2) {}};int n, m;vector<Edge> edges;vector<int> G[maxn];int vis[maxn];int ans;void init(){ for(int i = 0; i <= n; i++) G[i].clear(); edges.clear(); memset(vis, 0, sizeof(vis)); ans = inf;}void AddEdge(int from, int to, int cc, int dist, int dist2){ edges.push_back(Edge(from, to, cc, dist, dist2)); m = edges.size(); G[from].push_back(m-1);}void dfs(int u, int d){ if(d > ans) return ; if(u == n) { ans = ans>d ? d : ans; return; } for(int i = 0; i < G[u].size(); i++) { Edge e = edges[G[u][i]]; if(vis[e.to]<=m/2) { vis[e.to]++; dfs(e.to, d+e.dist); if(vis[e.cc]) dfs(e.to, d+e.dist2); vis[e.to]--; } }}int main(){ int M; while(~scanf("%d%d", &n, &M)) { init(); int a, b, c, d, d2; for(int i = 0; i < M; i++) { scanf("%d%d%d%d%d", &a, &b, &c, &d2, &d); AddEdge(a, b, c, d, d2); } dfs(1, 0); if(ans == inf) printf("impossible\n"); else printf("%d\n", ans); } return 0;}
- poj_3411 Paid Roads(dfs技巧)
- POJ 3411 Paid Roads(dfs技巧)
- Paid Roads(DFS)
- Paid Roads(DFS)
- POJ 3411 Paid Roads(dfs)
- poj 3411 Paid Roads(dfs)
- POJ 3411 Paid Roads(DFS)
- poj 3411 Paid Roads(DFS)
- (POJ 3411)Paid Roads <DFS>
- poj3411 Paid Roads---dfs
- POJ3411 Paid Roads DFS
- Paid Roads----DFS
- POJ 3411-Paid Roads(DFS)
- POJ 3411 Paid Roads(DFS)
- poj 3411 Paid Roads -dfs
- POJ 3411 Paid Roads (DFS多次访问节点)
- POJ 3411 Paid Roads(搜索的小技巧)
- poj 3411 Paid Roads 简单的dfs
- Linux Shell echo
- Excel数据分析与业务建模_第三章_引用函数INDEX(语法详解及应用实例)
- java序列化(Serializable)的作用和反序列化(转)
- 三、Java数组
- 【C++】学习笔记二十七——读取数字的循环
- poj_3411 Paid Roads(dfs技巧)
- IOC前世今生
- PAT. basic level. 1002
- 如何使用Alfred提升操作效率(上)
- [机房考试] 图论
- 苏嵌33 17.01.18
- LINUX ECHO 颜色
- 嵌入式数据结构
- 【POJ 2236 Wireless Network】 + 并查集