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

http://poj.org/problem?id=3411

大致题意:n个城市由m条公路连接,两个城市之间可能有多条公路连接。经过每条公路都需要收费,对于城市a,b,若之前经过城市c那么只需交p元钱,否则交r元钱。问从城市1到n的最小花费。

思路:由于经过每条公路的收费有两种方式,那么有的城市可能要经过多次,以便获得更小的花费,但也有可能出现有环的情况,那么该城市经过多次只会徒增花费。所以我们定义vis[]数组标记该城市访问的次数,在该题中(m <= 10)当一个城市访问超过3次(网上说这个与图的边数m有关)就可判定它在环中,没必要继续搜了。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#define LL long long
#define _LL __int64
#define eps 1e-8
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 110;

struct node
{
	int v,c,p,r;
};
vector <node> edge[20];

int n,m;
int vis[20];
int ans;

void dfs(int u, int cost)
{
	if(cost >= ans)
		return;

	if(u == n)
	{
		if(ans > cost)
			ans = cost;
		return;
	}

	for(int i = 0; i < (int)edge[u].size(); i++)
	{
		int v = edge[u][i].v;
        if ( vis[v] <= 3 ) //搜索的限制条件,避免一个点走过超过3次出现环路。之前没加RE了。
        {
        	vis[v]++;
            if ( vis[edge[u][i].c])
				dfs(v, cost + edge[u][i].p);
            else
				dfs(v, cost + edge[u][i].r);
			vis[v]--;
        }
	}
}

int main()
{
	int a,b,c,p,r;
	while(~scanf("%d %d",&n,&m))
	{
		for(int i = 0; i <= n; i++)
			edge[i].clear();
		for(int i = 0; i < m; i++)
		{
			scanf("%d %d %d %d %d",&a,&b,&c,&p,&r);
			edge[a].push_back((struct node){b,c,p,r});
		}
		memset(vis,0,sizeof(vis));
		ans = INF;
		vis[1] = 1;
		dfs(1,0);
		if(ans == INF)
			printf("impossible\n");
		else printf("%d\n",ans);
	}
	return 0;
}

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

时间: 2024-08-04 23:53:06

poj 3411 Paid Roads(dfs,可重复访问节点)的相关文章

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 (dfs)

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

多次访问节点的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 题解 《挑战程序设计竞赛》

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

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 on

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 1699 Best Sequence (DFS+预处理)

题意:看那张图就一清二楚了吧, N个序列首位相连(相同的序列部分),得到最短的总序列. 题目链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾相连能重合的长度求粗来.然后DFS枚举每种首尾相连的情况. #include<cstdio> #include<cstring> #include<algorithm> #define N 22 #define INF 0x7fffffff using namespace std