您的位置:首页 > 大数据 > 人工智能

poj 3411 Paid Roads

2015-08-03 13:51 405 查看


poj 3411 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 ai, 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, Pi ≤ Ri (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 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

一个点可以重复访问,到达的次数不超过4,其实可以算的不那么准,设成5、6、7都行,都证明在循环。但是时间会变长。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn=  10  ;
const int maxm=  10  ;
//const int INF=    ;
//typedef long long ll;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int e_max;
int u[maxm+3],v[maxm+3],need[maxm+3],w1[maxm+3],w2[maxm+3],nex[maxm+3];
int first[maxn+3];
int vis[maxn+3];
int n,m,ans;
inline void add_edge(int s,int t,int nee,int w11,int w22)
{
    int e=e_max++;
    u[e]=s;
    v[e]=t;
    need[e]=nee;
    w1[e]=w11;
    w2[e]=w22;

    nex[e]=first[s];
    first[s]=e;

}

void dfs(int x,int cost)
{

    for(int e=first[x];~e;e=nex[e])
    {
        int y=v[e];
        int nee=need[e];
        int add;
        if(vis[nee])  add=w1[e];
        else     add=w2[e];

     if(y==n)    {    ans=min(ans,cost+add);continue;         } //这里要写continue;而不能是return;在这里wa了好几次
        if(++vis[y]    >4  || cost>ans)  {vis[y]--;continue;}

         dfs(y,cost+add);
        vis[y]--;

    }

}

int main()
{
    int x,y,z,w11,w22;
    while(~scanf("%d%d",&n,&m))
    {

        e_max=0;
        memset(first,-1,sizeof first);
        for(int  i=1;i<=m;i++)
        {
            scanf("%d%d%d%d%d",&x,&y,&z,&w11,&w22);
             add_edge(x,y,z,w11,w22);

        }
        if(n==1)  {puts("0");continue;}
        memset(vis,0,sizeof vis);

        vis[1]=1;
        ans=INT_MAX;
         dfs(1,0);
         if(ans!=INT_MAX)
         printf("%d\n",ans);
         else
            puts("impossible");
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: