首页 > 其他 > 详细

【DFS模拟SPFA】P1993 小K的农场

时间:2019-10-09 14:51:27      阅读:200      评论:0      收藏:0      [点我收藏+]
技术分享图片
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<vector>
 5 using namespace std;
 6 
 7 vector<pair<int,int>>nextval[10010];
 8 bool vis[10010];
 9 const int INF = 0x3f3f3f3f;
10 int dis[10010];
11 int n, m;
12 int cnt;
13 
14 
15 bool spfa(int node)
16 {
17     vis[node] = true;
18     for (int i = 0; i < nextval[node].size(); i++)
19     {
20         if (dis[nextval[node][i].first] < dis[node] + nextval[node][i].second)
21         {
22             dis[nextval[node][i].first] = dis[node] + nextval[node][i].second;
23             if (vis[nextval[node][i].first]) return false;
24             if (!spfa(nextval[node][i].first)) return false;
25         }
26     }
27     vis[node] = false;
28     return true;
29 }
30 
31 int main()
32 {
33     cin >> n >> m;
34     for (int i = 1; i <= m; i++)
35     {
36 
37         int a, b, c, d;
38         cin >> a >> b >> c;
39         if (a == 1 || a == 2) cin >> d;
40         if (a == 1)
41         {
42             pair<int, int>t(b, d);
43             nextval[c].push_back(t);
44         }
45         if (a == 2)
46         {
47             pair<int, int>t(c, -d);
48             nextval[b].push_back(t);
49         }
50         if (a == 3)
51         {
52             pair<int, int>t1(b, 0);
53             pair<int, int>t2(c, 0);
54             nextval[b].push_back(t2);
55             nextval[c].push_back(t1);
56         }
57     }
58     bool flag = false;
59     for (int i = 1; i <= n; i++)
60     {
61         if (!spfa(i)) flag = true;
62     }
63     if (flag) cout << "No";
64     else cout << "Yes";
65     return 0;
66 }
View Code

 

【DFS模拟SPFA】P1993 小K的农场

原文:https://www.cnblogs.com/thjkhdf12/p/11641294.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!