• Paid Roads


    Problem 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, 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 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
     **************************************************************************************
    dfs&&标记
    ***************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstdio>
     7 #include<queue>
     8 #include<vector>
     9 #include<stack>
    10 using namespace std;
    11 const  int inf=1<<28;
    12 struct  node
    13 {
    14     int u,v,c,p,r;
    15 };
    16 vector<struct node>g[120];
    17 int vis[1001];
    18 int n,m,i,j,k;
    19 int ans;
    20 int u,v,c,p,r;
    21 void dfs(int x,int val)
    22  {
    23      vis[x]++;//访问次数
    24      if(x==n)
    25       {
    26           if(ans>val)//到底返回
    27            ans=val;
    28           return;
    29       }
    30       if(ans<val)
    31        return;
    32       for(int it=0;it<g[x].size();it++)
    33        {
    34            int v=g[x][it].v;
    35            int c=g[x][it].c;
    36            if(vis[v]<=3)//子节点访问不多于三次
    37             {
    38                 int min=inf;
    39                 if(vis[c]&&min>g[x][it].p)
    40                   min=g[x][it].p;
    41                 if(min>g[x][it].r)
    42                   min=g[x][it].r;
    43                 dfs(v,val+min);
    44                 vis[v]--;//恢复
    45             }
    46        }
    47  }
    48  int main()
    49  {
    50      cin>>n>>m;
    51      for(i=1;i<=m;i++)
    52       {
    53           cin>>u>>v>>c>>p>>r;
    54           g[u].push_back((struct node){u,v,c,p,r});//存储(重点)
    55       }
    56       memset(vis,0,sizeof(vis));
    57       ans=inf;
    58       dfs(1,0);
    59       if(ans==inf)
    60        cout<<"impossible"<<endl;
    61        else
    62         cout<<ans<<endl;
    63       return 0;
    64  }
    View Code
  • 相关阅读:
    Friends ZOJ
    2^x mod n = 1 HDU
    Paint the Grid Reloaded ZOJ
    Treap 模板
    bzoj进度条
    。。。
    bzoj
    。。。
    bzoj
    题解continue
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3273416.html
Copyright © 2020-2023  润新知