poj 3411 Paid Roads -dfs
2016-10-28 21:56
435 查看
Paid Roads
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
题目大意
求从1号城市到n号城市费用最小。由a-b城市交费用按如下计算方式
1:假如之前经过城市c就可以选择在城市c交 p块钱
2:在b城市交R块钱
解题思路
看了题解才明白题意,如果之前经过c城市那么可以选择交p或者交r块钱,否则只能交r块钱。
这样就会产生一个城市走很多次反而钱数变少的情况。具体不再举例
可是不可能一个城市走无数次,肯定得有个上限。题目规定n<=10,一个城市最多走4次,再走城市数就>10 了;为什么是4?见图
所以用一个visit数组存城市经过次数, 超过4次就不搜了。
代码
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn = 11;
const int INF = 0x3f3f3f3f;
struct edge{
int b,c,p,r;//经过了用p 否则用r
};
vector <edge> g[maxn];
void add_edge(int a,int b,int c,int p,int r)
{
g[a].push_back((edge){b,c,p,r});
}
int n,m;
int vist[maxn];
int ans = INF;
void dfs(int a,int f)
{
if(a==n)//只要找到终点了就没搜的必要了,费用越加越多
{
if(ans>f) ans = f;
return ;
}
for(int i= 0;i<g[a].size();++i)
{
edge e = g[a][i];
if(vist[e.b]<=4)//因为城市的个数是不超过10个,所以假如一个城市走了4遍了,说明有10个城市了
{
vist[e.b]++;
if(vist[e.c])
dfs(e.b,f+e.p);
else
dfs(e.b,f+e.r);
vist[e.b]--;
}
}
return ;//这个点连得所有边都找完了还没找到终点,返回上一层找下一个点;
}
int main()
{
int a,b,c,r,p;
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i)
{
scanf("%d%d%d%d%d",&a,&b,&c,&p,&r);
add_edge(a,b,c,p,r);
}
dfs(1,0);
if(ans == INF) printf("impossible");
else
printf("%d",ans);
return 0;
}
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
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
题目大意
求从1号城市到n号城市费用最小。由a-b城市交费用按如下计算方式
1:假如之前经过城市c就可以选择在城市c交 p块钱
2:在b城市交R块钱
解题思路
看了题解才明白题意,如果之前经过c城市那么可以选择交p或者交r块钱,否则只能交r块钱。
这样就会产生一个城市走很多次反而钱数变少的情况。具体不再举例
可是不可能一个城市走无数次,肯定得有个上限。题目规定n<=10,一个城市最多走4次,再走城市数就>10 了;为什么是4?见图
所以用一个visit数组存城市经过次数, 超过4次就不搜了。
代码
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn = 11;
const int INF = 0x3f3f3f3f;
struct edge{
int b,c,p,r;//经过了用p 否则用r
};
vector <edge> g[maxn];
void add_edge(int a,int b,int c,int p,int r)
{
g[a].push_back((edge){b,c,p,r});
}
int n,m;
int vist[maxn];
int ans = INF;
void dfs(int a,int f)
{
if(a==n)//只要找到终点了就没搜的必要了,费用越加越多
{
if(ans>f) ans = f;
return ;
}
for(int i= 0;i<g[a].size();++i)
{
edge e = g[a][i];
if(vist[e.b]<=4)//因为城市的个数是不超过10个,所以假如一个城市走了4遍了,说明有10个城市了
{
vist[e.b]++;
if(vist[e.c])
dfs(e.b,f+e.p);
else
dfs(e.b,f+e.r);
vist[e.b]--;
}
}
return ;//这个点连得所有边都找完了还没找到终点,返回上一层找下一个点;
}
int main()
{
int a,b,c,r,p;
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i)
{
scanf("%d%d%d%d%d",&a,&b,&c,&p,&r);
add_edge(a,b,c,p,r);
}
dfs(1,0);
if(ans == INF) printf("impossible");
else
printf("%d",ans);
return 0;
}
相关文章推荐
- 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技巧)
- POJ 3411 Paid Roads(DFS)
- POJ 3411 Paid Roads(DFS)
- POJ 3411 Paid Roads(dfs)
- POJ 3411 Paid Roads(DFS)
- poj 3411Paid Roads(走重复点的dfs )
- Poj 3411 Paid Roads【Dfs】
- POJ 3411 Paid Roads(SPFA || DFS)
- POJ 3411 Paid Roads 简单DFS
- poj 3411 Paid Roads很水的DFS
- poj 3411 Paid Roads(dfs)
- 多次访问节点的DFS POJ 3411 Paid Roads
- poj 3411 Paid Roads(dfs,可重复访问节点)
- poj 3411 Paid Roads(dfs+标记路径->求最少花费)
- poj 3411 Paid Roads 简单的dfs
- poj 3411 Paid Roads(DFS)