Paid Roads--POJ 3411

1、解题思路:深度搜索,位运算。

2、注意事项:题目理解为只要之前经过了Ci城市都可以取值Pi,注意Pi<=Ri用于判断;用位运算标示第i个城市是否访问过;递归中注意控制阀值为1、2、3都可以,但是1可以AC是因为数据不强,当取阀值为5的时候,提交将会超时。

3、实现方法:

1 #include<iostream>
2  using namespace std;
3
4  struct Node
5 {
6 int a,b,c,p,r;
7 };
8
9 Node edge[11];
10 int n,k,tot,ans;
11 int use[11];
12
13 void DFS(int i,int money,int dp)
14 {
15 if(edge[i].b==n)
16 {
17 if(money<tot)
18 tot=money;
19 return ;
20 }
21 for(int j=1;j<=k;j++)
22 {
23 if(edge[j].a==edge[i].b&&use[j]<=1)
24 {
25 use[j]++;
26 int min=edge[j].r;
27 int tmp1=1<<(edge[j].a-1);
28 int tmp2=1<<(edge[j].b-1);
29 int tmp3=1<<(edge[j].c-1);
30 if((dp|tmp3)==dp&&edge[j].p<edge[j].r)
31 min=edge[j].p;
32 DFS(j,money+min,(dp|tmp2));
33 use[j]--;
34 }
35 }
36 }
37
38 int main()
39 {
40 int i,tmp1,tmp2;
41 cin>>n>>k;
42 for(i=1;i<=k;i++)
43 cin>>edge[i].a>>edge[i].b>>edge[i].c>>edge[i].p>>edge[i].r;
44 if(n==1)
45 {
46 cout<<0<<endl;
47 return 0;
48 }
49 ans=100000;
50 for(i=1;i<=k;i++)
51 {
52 tot=100000;
53 if(edge[i].a==1)
54 {
55 use[i]++;
56 tmp1=1<<(edge[i].b-1);
57 tmp2=1<<(edge[i].a-1);
58 DFS(i,edge[i].r,tmp1|tmp2);
59 use[i]--;
60 }
61 if(tot<ans)
62 ans=tot;
63 }
64 if(ans==100000)
65 cout<<"impossible"<<endl;
66 else
67 cout<<ans<<endl;
68 return 0;
69 }

 

 

 

posted @ 2010-07-24 09:45  勇泽  阅读(1255)  评论(2编辑  收藏  举报