Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5481 | Accepted: 1947 |
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 51 2 1 10 102 3 1 30 503 4 3 80 802 1 2 10 101 3 2 10 50
Sample Output
110
有n个城市m条路线。每条路线有5个值,a,b,c,p,r表示从a城到b城。假设到过c城,收费p元,否则收费r元。
如今从1到n,求最小花费。
这题主要是去过的点又回来的问题,比如例子1->2->1->3->4,从2又返回了1,因为m=10,所以一个点最多訪问3次(事实上取2也能A,感觉还是3靠谱点)
ps:刚開始记录了map[i][j]表示i到j能够走第map[i][j]条路。由于从第i到j能够有多条路,一直wa,再改就麻烦了,换成直接枚举m条路,反正m也不大。
代码:
#include#include #include #include using namespace std;const int inf=99999999;//int map[15][15];int vis[15];int n,m;int ans;struct node{ int a; int b; int c; int p; int r;}road[15];void dfs(int u,int v){ if(v>ans) return; if(u==n) { if(v