poj3411 Paid Roads

思路:

搜索。注意点和边都有可能经过多次。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 const int MAXN = 15, INF = 0x3f3f3f3f;
 6 int n, m;
 7 struct edge
 8 {
 9     int to, c, p, r;
10 };
11 vector<edge> G[MAXN];
12 int vis[MAXN];
13 
14 int dfs(int now)
15 {
16     if (now == n)
17         return 0;
18     int ans = INF;
19     for (int i = 0; i < G[now].size(); i++)
20     {
21         if (vis[G[now][i].to] > 3) continue;
22         vis[G[now][i].to]++;
23         int x = dfs(G[now][i].to) + G[now][i].r;
24         ans = min(ans, x);
25         int y = INF;
26         if (vis[G[now][i].c])
27             y = dfs(G[now][i].to) + G[now][i].p;
28         ans = min(ans, y);
29         vis[G[now][i].to]--;
30     }
31     return ans;
32 }
33 
34 int main()
35 {
36     cin >> n >> m;
37     int a, b, c, p, r;
38     for (int i = 0; i < m; i++)
39     {
40         cin >> a >> b >> c >> p >> r;
41         G[a].push_back(edge{b, c, p, r});
42     }
43     vis[1] = true;
44     int ans = dfs(1);
45     if (ans == INF) puts("impossible");
46     else cout << ans << endl;
47     return 0;
48 }

 

转载于:https://www.cnblogs.com/wangyiming/p/6994567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值