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 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 ≤ 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 ≤ 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是你的出发点 ,从a到b价格为R,但如果之前你已经到过c,那么从a到b价格降为P。样例中,1->2->1->3->4,总共10+10+10+80=110。一条路可以走多次,所以这里用到了结构体容器,一个点的信息存放在一个容器中,来搜索。第一次用容器……好强大^_^!


AC代码

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define N 11
#define INF 1000000

struct NODE
{
    int v, c, p, r;
}temp;
vector<NODE> node[N];                             //定义结构体的容器,N个容器
int vis[N], ans, n, m;

int dfs(int u, int cost)                         //目前所在点u,和话费cost
{
    vis[u]++;
    if( cost > ans )  return 0;                 //最小花费不能更新时返回上层

    if( u == n )
    {
        if( ans>cost )
            ans=cost;
        return 0;                              //已经到达n点目的地,更新最小花费,返回上层,继续搜索
    }

    int i, v, t;
    for( i=0; i<node[u].size(); i++ )
    {
        v=node[u][i].v;
        if( vis[v] <= 3 )
        {
            t=INF;
            if( vis[ node[u][i].c ] )        //如果c已经拜访过,收费node[u][i].p,否则收费node[u][i].r
                t=node[u][i].p;
            else
                t=node[u][i].r;
            dfs( v, t+cost );               //v为出发点,已花费t+cost
            vis[v]--;
        }
    }
    return 0;
}


int main()
{
    int a, b, c, p, r;
    scanf("%d%d",&n,&m);
    memset(vis, 0, sizeof(vis));
    while( m-- )
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&p,&r);
        temp.v = b;
        temp.c = c;
        temp.p = p;
        temp.r = r;
        node[a].push_back(temp);                   //赋值,初始化容器中的结构体
    }

    ans=INF;
    dfs(1, 0);
    if( ans != INF )  printf("%d\n",ans);
    else  printf("impossible\n");

    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值