ACM: 深搜题 poj 3411
2016-05-19 23:18
239 查看
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
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
题意: 现在给你n个城市m条路的图. 部分路要收费的. 收费方式有2种: 经过Ci城市的就Pi , 否则Ri.
现在要你求出最小的费用从1 到 n城市.
解题思路:
1. 条件最短路. 标记神搜即可了.
2. 每个点经过不超过三次.(关键,使深搜树有了最下层)
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 15
const int INF = 1 << 30;
struct node
{
int v;
int c;
int p;
int r;
int
next;
}edges[MAX];
int n, m;
int next[MAX];
int vis[MAX];
int result;
int num;
void init()
{
num =
0;
result =
INF;
memset(next,-1,sizeof(next));
memset(vis,0,sizeof(vis));
}
void dfs(int v,int w)
{
if(w
> INF)
return ;
if(v ==
n)
{
if(result > w)
result = w;
return ;
}
for(int e =
next[v]; e != -1; e = edges[e].next)
{
int c = edges[e].c;
int p = edges[e].p;
int r = edges[e].r;
if(vis[ edges[e].v ] <= 2)
{
vis[ edges[e].v ]++;
if(vis[c] > 0)
dfs(edges[e].v,w+p);
else
dfs(edges[e].v,w+r);
vis[ edges[e].v ]--;
}
}
}
int main()
{
//
freopen("input.txt","r",stdin);
int a, b, c,
p, r;
while(scanf("%d
%d",&n,&m) != EOF)
{
init();
for(int i = 1; i <= m; ++i)
{
scanf("%d %d %d %d
%d",&a,&b,&c,&p,&r);
edges[num].v = b;
edges[num].c = c;
edges[num].p = p;
相关文章推荐
- ACM: 条件最短路 poj 1724 (没剪枝…
- ACM: 有难度的深搜题 poj 3373
- ACM: 模拟题 poj 2993
- Android中的异步任务
- ACM: hash题 poj 3274 (题目看了好…
- ACM: dp题(动态规划) poj 2151 (d…
- ACM: dp题 poj 1276 go on 动态规…
- ACM: 简单数塔题 dp题 poj 3176
- ACM: dp动态规划题(难) poj 3267 …
- ACM: 最优解 dp题 poj 1260
- ACM: 最长升序和最长降序 dp题 poj…
- QuickContactBadge的学习 快速关联联系人
- NFS服务器的构建与使用
- -bash: /tyrone/jdk/jdk1.8.0_91/bin/java: cannot execute binary file
- Mysqlite的基本用法
- 5 多线程,原子操作
- Jmeter(4)Http Cookie Manager
- ACM: 最长子序 dp题 poj 2533
- ACM: 模拟题 poj 2632 (开始做模拟…
- ACM: 计算交换次数的排序题 poj 22…