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