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 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


大致题意:

有n座城市和m(1<=n,m<=10)条路。现在要从城市1到城市n。有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱,如果没有去过就付R的钱。求的是最少要花多少钱。

注意:路径是有向的。

 

 

解题思路:

DFS。这题当有了思路后,做起来是没有难度的,但是思维推算能力要求很高。

 

这题难点在于“城市与城市之间可能存在多条路径”:

1、  输入数据时可能会出现多条 从城市a到城市b的路径信息,但是费用有所差别;

2、  对于 从城市a到城市b 的同一条路径,允许重复走。

 

有人会问,重复走同一条路径有什么意义?单纯增加费用而已,为什么不能标记所有路径,每条路只允许走一次,这样费用不是更少么?

我开始也是陷入了这种思维,但是这种想法其实“对一半,错一半”。

先来看一组数据:

6 5

1 2 1 10 10

2 3 4 10 100

2 4 2 15 15

4 1 1 12 12

3 6 6 10 10

如果每条路只允许走一次,那么方案只有1个:

1->2->3->6 共135元

但这组数据的正确答案是67元。为什么?正确的方案如下:

1->2->4->1->2->3->6 共67元

显然1->2重复走了一次,目的是为了先到达城市4,从而使得2->3这段路的费用从100缩减到10元。

 

       看到这里很多同学好像就恍然大悟,但是问题马上又来了。如果同一条路允许重复走,那么就不能标记了,但一旦不标记,失去了搜索的限制条件,DFS就无法结束,不是陷入死循环了?

       我刚才说这种思路“对一半,错一半”,“对”是对在“重复走会增加费用”,“错”是错在“重复走的对象不是某一条路,而是某一个环路”。在同一个环路重复走才会真正增加费用。但是标记环路是很麻烦的,那么能不能根据某一条路或某一个城市重复走过的次数来判断当前所走的方案已经出现了环路? 答案是可以的。

       上述的例子已经验证过了,同一条路可以重复走,但是不能无限重复走,重复的次数是有限的。那么应该重复多少次才合理?这与m值有关。题目的m值范围为<=10,那么当人一个城市被到达的次数若  >3次(不包括3),所走的方案必然出现了环路(网上的同学称之为“闸数”)。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <deque>
#include <stack>
//#define m 0x7fffffff

using namespace std;

struct node
{
    int a,b,c,d,e;
}map[15];

int n,m,minn;
int flag[15];

void dfs(int pos,int sum)
{
    if(pos == n)
    {
        if(sum<minn)
        {
            minn=sum;

        }
        return ;
    }
    for(int i=1;i<=m;i++)
    {
        if(pos == map[i].a && flag[map[i].b] <= 3)
        {
//            int b=;
            flag[map[i].b]++;

            if(flag[map[i].c])
                dfs(map[i].b,sum+map[i].d);
            else
                dfs(map[i].b,sum+map[i].e);
            flag[map[i].b]--;
        }
    }
    return ;
}

int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        {
            cin>>map[i].a>>map[i].b>>map[i].c>>map[i].d>>map[i].e;
        }
        minn=0x7ffffff;
        flag[1]=1;
        dfs(1,0);
        if(minn == 0x7ffffff)
            cout<<"impossible"<<endl;
        else
            cout<<minn<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值