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

畫船聽雨發表於2014-04-10

題意:給你n個點,m條邊。每條邊裡面有a, b, c, r, p;代表從a到b如果c點經過了的話。那就要花費p元,否則花費r元。

需要注意的是:可能有環,所有每個點經歷的次數會大於1次。所以要加次數的上限。

用到了優先佇列,這樣保證第一次找到就是最短的。

Paid Roads
Time Limit: 1000MS   Memory Limit: 65536K
Total 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 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
#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.1415926535898

using 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;
}


相關文章