poj3411Paid Roads

来源:互联网 发布:淘宝汽车装饰安装联盟 编辑:程序博客网 时间:2024/05/01 07:33

题目链接:

http://poj.org/problem?id=3411

题目:

Paid Roads
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5080 Accepted: 1784

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:如果以前经过c城市,那么就在c城市付费。。

2:如果没有经过,那么就在b城市付费。。

还有注意题目中的数据为m<10,所以每个点最多走3次。。

因为成环的话那么最少3个点,则最少3路。所以最多走3次。。。

故应用int vis[ manx]。。。

然后dfs回溯。。。

所以代码为:

#include<iostream>#include<cstdio>#include<cstring>#define INF 0x3f3f3f3fconst int maxn=10+10;int vis[maxn];int min_cost,fee;int n,m;struct node{    int a,b,c,p,r;}e[maxn];void dfs(int x,int fee){    if(x==n&&min_cost>fee)    {        min_cost=fee;        return;    }    if(x==n)        return;    for(int i=1;i<=m;i++)    {        if(e[i].a==x&&vis[e[i].b]<3)        {            vis[e[i].b]++;            if(vis[e[i].c])                dfs(e[i].b,fee+e[i].p);            else                dfs(e[i].b,fee+e[i].r);            vis[e[i].b]--;        }    }}int main(){   while(scanf("%d %d",&n,&m)!=EOF)   {       memset(vis,0,sizeof(vis));       min_cost=INF;       for(int i=1;i<=m;i++)       {           scanf("%d%d%d%d%d",&e[i].a,&e[i].b,&e[i].c,&e[i].p,&e[i].r);       }       vis[1]=1;       dfs(1,0);       if(min_cost!=INF)          printf("%d\n",min_cost);       else         printf("impossible\n");   }   return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三个月婴儿流清鼻涕怎么办 宝宝7个月流鼻涕怎么办 6个月孩子流鼻涕怎么办 十一个月孩子感冒流鼻涕怎么办 7个月孩子流鼻涕怎么办 18个月的宝宝流鼻涕怎么办 6个月宝宝咳嗽流鼻涕怎么办 18个月宝宝咳嗽流鼻涕怎么办 6个月宝宝流鼻涕打喷嚏怎么办 宝宝18个月流鼻涕怎么办 18个月宝宝感冒流鼻涕怎么办 小孩睡觉鼻子不通气怎么办 宝宝鼻子里有鼻涕出不来怎么办 孩子喉咙有痰呼呼响怎么办 六个月宝宝鼻塞怎么办速效办法 两个月小孩鼻子不通气怎么办 一岁宝宝流清鼻涕怎么办 孩子鼻子里有鼻涕怎么办 宝宝晚上睡觉鼻子不通气怎么办 宝宝感冒睡觉鼻子不通气怎么办 宝宝3个月流鼻涕怎么办 小孩吃着了发烧怎么办 半岁宝宝鼻子塞怎么办 宝宝伤风鼻子不通气怎么办 二十天的宝宝伤风鼻子不通怎么办 一个多月的宝宝鼻子有鼻屎怎么办 2个月宝宝鼻子里有鼻屎怎么办 四个月婴儿感冒发烧怎么办 一个月婴儿感冒发烧怎么办 五个月婴儿感冒发烧怎么办 两个月的婴儿感冒发烧怎么办 9个月婴儿感冒发烧怎么办 两个月婴儿感冒鼻塞咳嗽怎么办 两个月婴儿感冒咳嗽流鼻涕怎么办 小孩感冒发烧怎么办速效办法 宝宝打喷嚏流清鼻涕怎么办 宝宝感冒流清鼻涕怎么办 2岁感冒流清鼻涕怎么办 小孩感冒流清鼻涕怎么办 4岁宝宝半夜发烧怎么办 四岁宝宝免疫力低下怎么办