POJ 3411 Paid Roads

题目来源:http://poj.org/problem?id=3411

Paid Roads

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5383   Accepted: 1923

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 ifrom 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 aibiciPiRi (1
≤ ≤ 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
≤ ≤ 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

Source

Northeastern Europe 2002, Western Subregion

题意: 一共有编号从1~N的N个城市,城市之间有m条道路,一些路是要收费的。如果从ai到bi之前有经过ci城市,则收Pi的钱,否则收Ri的钱。求从城市1到城市N最小的花费。

题解: DFS~~  搜索所有的路径,点可以重复走~~  但是有个约束条件,一个点顶多走三次,不然就形成环出不来 了~~

PS: 开始题目意思没看懂....囧.........

AC 代码:

#include<iostream>
#define Max 9999999
using namespace std;
int map[11][5],n,m,count=0,res=Max;
int p[11],pt[11]={0};
void dfs(int k){
	if(k==n){
		if(count<res) res=count;
		return ;
	}
	for(int i=1;i<=m;i++)
	if(k==map[i][0]&&pt[map[i][0]]<3){
		count+=map[i][p[map[i][2]]+3];
		int tempx=p[map[i][0]];
		p[map[i][0]]=0;
		pt[map[i][0]]++;
		if(count<res)
		dfs(map[i][1]);
		pt[map[i][0]]--;
		p[map[i][0]]=tempx;
		count-=map[i][p[map[i][2]]+3];
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++) p[i]=1;
	for(int i=1;i<=m;i++)
	cin>>map[i][0]>>map[i][1]>>map[i][2]>>map[i][3]>>map[i][4];
	dfs(1);
	if(res!=Max)
	cout<<res<<endl;
	else cout<<"impossible\n";
	return 0;
}

时间: 2024-08-01 08:58:28

POJ 3411 Paid Roads的相关文章

POJ 3411 Paid Roads(dfs)

*注:这一题很重要的是对与数据的处理和细节的把握把! http://poj.org/problem?id=3411 题目大意: 有n个城市,m条路,(0<=n,m<=10).从a到b,如果之前已经经过c点,那么付费p,否者付费r.求最小的费用,从1-->n! 注意: There may be more than one road connecting one city with another. so:你不能用map[a][b]存a->b的距离.只能有road [ i ]了. 还有

POJ 3411 Paid Roads 题解 《挑战程序设计竞赛》

POJ 3411 Paid Roads开路:N个城市间有m条单向路,分别从a到b,可以在c处交P路费,也可以直接交R路费.那么问题来了,你的挖掘机怎么开最省钱?3.4熟练掌握动态规划状态压缩DP乍一看可以Dijkstra,实际上的确可以Dijkstra.不过多了一个预交费的c,所以在遍历的时候多了一维状态,这个维度储存当前走过的城市集合.在Dijkstra的时候,如果走过了c那么就有两个选择,选其中最省的即可:否则没得选.#include <iostream> #include&nb.

多次访问节点的DFS POJ 3411 Paid Roads

POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 2430 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 o

poj 3411 Paid Roads (dfs)

题目链接 题意:有N个城市被M条道路连接起来了,每两个城市之间可能存在超过一条路,但是城市之间是单向连接的. 每条路是要花费的.每条路的花费可以选择两种方式:1:假如a城市到达b城市,如果之前经过了c城市,那么这条 路上的花费为P也可以为R.2:如果没有经过c,则这条路上的花费为R.问从城市1到城市n最小的花费是多少. 思路:存在走多次边的情况,所以vis[]数组可以多次,但是这个题目的多次的上限为3(不知道为什么). 1 #include <iostream> 2 #include <

poj 3411 Paid Roads(dfs,可重复访问节点)

http://poj.org/problem?id=3411 大致题意:n个城市由m条公路连接,两个城市之间可能有多条公路连接.经过每条公路都需要收费,对于城市a,b,若之前经过城市c那么只需交p元钱,否则交r元钱.问从城市1到n的最小花费. 思路:由于经过每条公路的收费有两种方式,那么有的城市可能要经过多次,以便获得更小的花费,但也有可能出现有环的情况,那么该城市经过多次只会徒增花费.所以我们定义vis[]数组标记该城市访问的次数,在该题中(m <= 10)当一个城市访问超过3次(网上说这个与

poj 3411 Paid Roads(dfs)

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

POJ 3411 Paid Roads(SPFA || DFS)

题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态压缩一下,第二维来记录状态,表示到过这个点的第几个状态.也可以用DFS,因为最多十个点,所以如果走某一个点走过三遍说明就是真的只增加费用了,也就是真正的在走环路了.DFS分析 二维SPFA 1 #include <stdio.h> 2 #include <string.h> 3 #in

广大暑假训练1 E题 Paid Roads(poj 3411) 解题报告

题目链接:http://poj.org/problem?id=3411 题目意思:N个city 由 m 条路连接,对于一条路(假设连接Cityia和 Cityb),如果从Citya 去 Cityb的途中,之前已经走过Cityc(可能会等于a),那么就可以交p的钱,否则之前未走过Cityc,就一定要交r 的路费啦. 注意,一个点可以被反复多次走,也就是可能构成环,虽然路走长了,但路费便宜了,这个问题要考虑到.还有就是剪枝啦:如果当前求得的路费比以前求得的答案要大,那就要回溯. mincost 明明

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e