poj_3411 Paid Roads(dfs技巧)

来源:互联网 发布:上海万国数据 编辑:程序博客网 时间:2024/05/17 06:06
Paid Roads
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6936 Accepted: 2574

Description

A network of m roads connects N cities (numbered from 1 toN). 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 roadi from city ai to city bi:

  • in advance, in a city ci (which may or may not be the same asai);
  • 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 ofai, 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,PiRi (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 cityN. 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
从城市a到城市b有若干条路,每条路都需要过路费,可以选择先在城市c交,也可以后到城市b交,费用不同。
求城市1到城市n的最少费用。
题目所构造的图有环,而且有环的方案可能更优,所以直接标记城市是否走过来dfs是不能求出答案的,因为有些城市可能走过不止一次。
那么可以考虑城市最多走过的次数,由于边数为m条,所以最多走过的次数应该是m/2。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 20#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Edge{    int from, to, cc, dist, dist2;    Edge(int a, int b, int c, int d, int d2) : from(a), to(b), cc(c), dist(d), dist2(d2) {}};int n, m;vector<Edge> edges;vector<int> G[maxn];int vis[maxn];int ans;void init(){    for(int i = 0; i <= n; i++) G[i].clear();    edges.clear();    memset(vis, 0, sizeof(vis));    ans = inf;}void AddEdge(int from, int to, int cc, int dist, int dist2){    edges.push_back(Edge(from, to, cc, dist, dist2));    m = edges.size();    G[from].push_back(m-1);}void dfs(int u, int d){    if(d > ans) return ;    if(u == n) { ans = ans>d ? d : ans; return; }    for(int i = 0; i < G[u].size(); i++)    {        Edge e = edges[G[u][i]];        if(vis[e.to]<=m/2)        {            vis[e.to]++;            dfs(e.to, d+e.dist);            if(vis[e.cc]) dfs(e.to, d+e.dist2);            vis[e.to]--;        }    }}int main(){    int M;    while(~scanf("%d%d", &n, &M))    {        init();        int a, b, c, d, d2;        for(int i = 0; i < M; i++)        {            scanf("%d%d%d%d%d", &a, &b, &c, &d2, &d);            AddEdge(a, b, c, d, d2);        }        dfs(1, 0);        if(ans == inf) printf("impossible\n");        else printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击