POJ 3411 Paid Roads(DFS)

来源:互联网 发布:mac长的破折号怎么打 编辑:程序博客网 时间:2024/05/16 08:47

题意  你要从第1个城市到第N个城市去   有m条路  每条路用a, b, c, p, r 表示  你从第a个城市到第b个城市时  若之前经过或现在位于第c个城市  过路费就是p元  否则就是r元   求你到达第N个城市最少用多少过路费

由于最多只有10个城市  10条路  这个题就变得很简单了  直接暴力dfs就行  可以用状态压缩来存储已经走过了哪些城市  由于最多只有10条路  从某个城市出发要一条  回这个城市也要一条  所以一个城市最多经过5次  这个可以作为dfs的结束条件

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 11, INF = 2333333;int n, m, ans, vis[N];struct road{    int a, b, c, p, r;} rd[N];//当前所在城市, 到过哪些城市, 当前已经用了多少过路费void dfs(int p, int s, int v){    if(vis[p] > 5) return;    s = s | 1 << (p - 1);    if(p == n)    {        ans = min(ans, v);        return;    }    for(int i = 0; i < m; ++i)    {        if(rd[i].a != p) continue;        ++vis[rd[i].b];        if(s & 1 << (rd[i].c - 1)) //到过城市c            dfs(rd[i].b, s, v + rd[i].p);        else dfs(rd[i].b, s, v + rd[i].r);        --vis[rd[i].b]; //回溯    }}int main(){    while(~scanf("%d%d", &n, &m))    {        for(int i = 0; i < m; ++i)            scanf("%d%d%d%d%d", &rd[i].a, &rd[i].b, &rd[i].c, &rd[i].p, &rd[i].r);        ans = INF;        memset(vis, 0, sizeof(vis));        dfs(1, 0, 0);        if(ans == INF) puts("impossible");        else printf("%d\n", ans);    }    return 0;}
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 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 51 2 1 10 102 3 1 30 503 4 3 80 802 1 2 10 101 3 2 10 50

Sample Output

110

Source

Northeastern Europe 2002, Western Subregion


1 0