POJ3411 DFS最小计算消费值
2016-01-30 19:54
295 查看
Description
A network of m roads connects N cities (numbered from 1 to N). 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
road i from city ai to city bi:
in advance, in a city ci (which may or may not be the same as ai);
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 of ai, 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 city N. If the trip is not possible for any reason, the line must contain the word ‘impossible’.
Sample Input
Sample Output
110
A network of m roads connects N cities (numbered from 1 to N). 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
road i from city ai to city bi:
in advance, in a city ci (which may or may not be the same as ai);
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 of ai, 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 city N. If the trip is not possible for any reason, the line must contain the word ‘impossible’.
Sample Input
4 5 1 2 1 10 10 2 3 1 30 50 3 4 3 80 80 2 1 2 10 10 1 3 2 10 50
Sample Output
110
</pre><pre name="code" class="cpp">#include <IOSTREAM> #include <CSTDLIB> using namespace std; #define MAX 11 #define MAXCOUNT 2000 typedef struct { int ai, bi, ci, Pi, Ri; }CITY; CITY city[MAX]; int visit[MAX]; int N, m; int min_cost; void search_dfs(int in_city, int cost_count) { if (in_city == N&&min_cost > cost_count) { min_cost = cost_count; return; } int ic; for (ic = 1; ic <= m; ic++) ////////// { if (in_city == city[ic].ai&&visit[city[ic].bi] <= 3) { visit[city[ic].bi]++; if (visit[city[ic].ci] > 0) //如果曾经到过城市C则用Pi的花费计算,否则用Ri的花费计算 search_dfs(city[ic].bi, cost_count + city[ic].Pi); else search_dfs(city[ic].bi, cost_count + city[ic].Ri); visit[city[ic].bi]--; } } return; } int main() { while (cin >> N >> m) { if ((!(N >= 1 && N <= 10)) || (!(m >= 1 && m <= 10))) continue; memset(city, 0, sizeof(city)); memset(visit, 0, sizeof(visit)); min_cost = MAXCOUNT; int ic; for (ic = 1; ic <= m; ic++) { cin >> city[ic].ai >> city[ic].bi >> city[ic].ci >> city[ic].Pi >> city[ic].R; } visit[city[ic].ai] = 1; search_dfs(1, 0); if (min_cost >= MAXCOUNT) cout << "impossible" << endl; else cout << min_cost << endl; } return 0; }
相关文章推荐
- linux基础命令练习
- linux程序的安装
- linux文件系统,用户,网络设置
- linux 基础知识总结-----基本命令
- JavaScript的学习整理(四)
- 【JS学习笔记】 匿名函数和闭包
- mysqladmin在SuSE linux系统中--sleep參数使用不准确问题
- 02Android用户界面优化之(一)Android Fragment
- Hive启动报错Caused by MetaException message Version information not found in metastore
- iOS 代码触发button点击事件 objective-c怎么代码触发按钮点击事件
- 定位 new 运算符
- 看Struts官方项目一些配置习惯
- (转)投票系统,更改ip刷票
- SweetAlert详解
- 递推 HDU 2048 神、上帝以及老天爷
- Linux中Matlab保存多个数据到同一个文件当中
- 在程序世界,寻找更多精彩——线程的启动
- 1037. 在霍格沃茨找零钱(20)
- 使用live555实时播放rtsp
- 在CSDN博客中输入数学公式