POJ 3411 Paid Roads

来源:互联网 发布:黄金列车 知乎 编辑:程序博客网 时间:2024/04/28 23:29
Paid Roads
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4573 Accepted: 1557

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

Source

Northeastern Europe 2002, Western Subregion
     题意很容易明白,可恨的是卡了好久。注意两点:
 (1)当n为1时,输出0; 
 (2)有的最优路径要包含相同的边 
测试数据:


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

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 2
1 2 3 10 10
3 1 3 10 10

3 2
1 2 2 10 20
2 3 2 10 20

输出:
110
67
0
30
impossible
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#define INF 0x7ffffff#define N 15using namespace std;struct num{    int end,c,p,r,next;}a[N];int Min,point[1000],b[N],Top,cord[N];int n,m;bool path[N];int main(){    //freopen("data1.in","r",stdin);    void addeage(int sta,int e,int c,int p,int r);    void dfs(int k,int tag);    while(scanf("%d %d",&n,&m)!=EOF)    {        memset(b,-1,sizeof(b));        Top=0;        for(int i=1;i<=m;i++)        {            int e,c,p,r,sta;            scanf("%d %d %d %d %d",&sta,&e,&c,&p,&r);            addeage(sta,e,c,p,r);        }        if(n==1)        {            printf("0\n");            continue;        }        Min=INF;        memset(cord,0,sizeof(cord));        point[0] = 1;        dfs(1,1);        if(Min==INF)        {            printf("impossible\n");        }else        {            printf("%d\n",Min);        }    }    return 0;}void addeage(int sta,int e,int c,int p,int r){    a[Top].end = e;    a[Top].c = c;    a[Top].p = p;    a[Top].r = r;    a[Top].next = b[sta];    b[sta] = Top;    Top++;}void dfs(int k,int tag){    for(int i=b[k]; i!=-1; i=a[i].next)    {        if(cord[i]<=3)        {            cord[i] +=1;            int e = a[i].end;            point[tag] = i;            if(e==n)            {                memset(path,false,sizeof(path));                path[1] = true;                int s = 0,sta,end=1;                for(int j=1;j<=tag;j++)                {                    sta = end;                    end = a[point[j]].end;                    int c = a[point[j]].c;                    int p = a[point[j]].p;                    int r = a[point[j]].r;                    if(path[c])                    {                       p<r? s+=p:s+=r;                    }else                    {                        s+=r;                    }                     path[end] = true;                }                Min = min(Min,s);            }else            {                dfs(e,tag+1);            }            cord[i] -=1;        }    }}