POJ 3411 Paid Roads(搜索的小技巧)

来源:互联网 发布:手机淘宝登录网页版 编辑:程序博客网 时间:2024/06/05 08:35

题意:给你n个点,m条边。每条边里面有a, b, c, r, p;代表从a到b如果c点经过了的话。那就要花费p元,否则花费r元。

需要注意的是:可能有环,所有每个点经历的次数会大于1次。所以要加次数的上限。

用到了优先队列,这样保证第一次找到就是最短的。

Paid Roads
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4985 Accepted: 1731

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
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 1000100#define LL __int64//#define LL long long#define INF 0x3fffffff#define PI 3.1415926535898using namespace std;const int maxn = 15;int head[maxn];int n, m, ans;int num[maxn];struct node1{    int b, c, p, r;    int next;} f[maxn];struct node{    int fa, sum;    bool vis[maxn];    bool operator < (const node &a) const    {        return a.sum < sum;    }};void add(int a, int b, int c, int p, int r){    f[ans].b = b;    f[ans].c = c;    f[ans].p = p;    f[ans].r = r;    f[ans].next = head[a];    head[a] = ans++;}void bfs(){    struct node temp, xtemp;    memset(temp.vis, false, sizeof(temp.vis));    memset(num, 0 , sizeof(num));    temp.sum = 0;    temp.fa = 1;    num[1]++;    temp.vis[1] = true;    priority_queue<node>que;    que.push(temp);    while(!que.empty())    {        temp = que.top();        que.pop();        if(temp.fa == n)            break;        if(num[temp.fa] > 24)            continue;        int p = head[temp.fa];        while(p !=  -1)        {            xtemp = temp;            xtemp.vis[f[p].b] = true;            if(!xtemp.vis[f[p].c])                xtemp.sum += f[p].r;            else                xtemp.sum += f[p].p;            xtemp.fa = f[p].b;            num[f[p].b]++;            que.push(xtemp);            p = f[p].next;        }    }    if(temp.fa != n)        cout<<"impossible"<<endl;    else        cout<<temp.sum<<endl;}int main(){    while(cin >>n>>m)    {        int a, b, c, p, r;        memset(head, -1, sizeof(head));        ans = 0;        for(int i = 0; i < m; i++)        {            cin >>a>>b>>c>>p>>r;            add(a, b, c, p, r);        }        bfs();    }    return 0;}


0 0