标签:des style blog http color os io strong
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10777 | Accepted: 3961 |
Description
Input
Output
Sample Input
5 6 7 1 2 2 3 2 4 3 3 3 4 2 4 1 3 4 1 4 6 2 1 3 5 2 0 5 4 3 2
Sample Output
11
Source
1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 using namespace std;
5
6 #define MAXN 110
7
8 struct Node{
9 int city;
10 int len;
11 int toll;
12 Node* next;
13 }city[MAXN]; //邻接表
14
15 bool isv[MAXN];
16
17 int K,n,r,s;
18 int Min;
19
20 void dfs(int c,int l,int k)
21 {
22 //k为剩下的钱
23 if(k<0)
24 return ;
25 if(l>=Min) //如果当前走过的路的长度超过了记录的最小值,则退出
26 return ;
27 if(c==n && l<Min){ //到达n城市
28 Min = l;
29 return ;
30 }
31
32 Node* p = city[c].next;
33 while(p){
34 if(!isv[p->city]){ //没走过
35 isv[p->city] = true;
36 dfs(p->city,l + p->len,k - p->toll); //进入下一个城市
37 isv[p->city] = false;
38 }
39 p = p->next;
40 }
41 }
42
43 int main()
44 {
45 while(scanf("%d",&K)!=EOF){
46 scanf("%d",&n);
47 scanf("%d",&r);
48
49 memset(city,NULL,sizeof(city));
50 memset(isv,0,sizeof(isv));
51 Min = 0x7ffffff;
52
53 while(r--){
54 scanf("%d",&s);
55 Node* p = (Node*)malloc(sizeof(Node));
56 scanf("%d%d%d",&p->city,&p->len,&p->toll);
57 p->next = city[s].next;
58 city[s].next = p;
59 }
60
61 isv[1] = true;
62 dfs(1,0,K);
63
64 if(Min==0x7ffffff)
65 printf("-1\n");
66 else
67 printf("%d\n",Min);
68 }
69 return 0;
70 }
Freecode : www.cnblogs.com/yym2013
poj 1724:ROADS(DFS + 剪枝),布布扣,bubuko.com
标签:des style blog http color os io strong
原文地址:http://www.cnblogs.com/yym2013/p/3891517.html