YSU小吃街

痞子三分冷 提交于 2019-12-06 12:52:48

貌似百度没题解

贪心是贪不过的,举个例子

5 4 10

1 2 2

2 4 3

3 4 2

4 5 3

还有注意不连通情况

 1 #include<iostream>
 2 #include<sstream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<string>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<functional>
 9 #include<iomanip>
10 #include<numeric>
11 #include<cmath>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #include<map>
16 #include<cctype>
17 const double PI = acos(-1.0);
18 const int INF = 0x3f3f3f3f;
19 const int NINF = -INF - 1;
20 const int maxn = 1e5 + 5;
21 typedef long long ll;
22 #define MOD 1000000007
23 using namespace std;
24 int n, m, st;
25 int vis[maxn];
26 int rec = 0;
27 typedef pair<int, int> P;
28 struct node
29 {
30     int next, cost;
31     int flag;
32 };
33 vector<node> G[maxn];
34 void bfs()
35 {
36     queue<P> q;
37     q.push(P(1, st));
38     while(q.size())
39     {
40         P p = q.front();
41         q.pop();
42         for(int i = 0; i < G[p.first].size(); ++i)
43         {
44             node tmp = G[p.first][i];
45             int mp;
46             if(!tmp.flag) mp = p.second - tmp.cost;
47             else mp = p.second + tmp.cost;
48             if (vis[tmp.next] == -1) {
49                 vis[tmp.next] = mp;
50                 q.push(P(tmp.next, vis[tmp.next]));
51             }
52             else{
53                 if(vis[tmp.next] != mp) {
54                     rec = 1;
55                     break;
56                 }
57             }
58         }
59         if(rec) break;
60     }
61 }
62 int main()
63 {
64     memset(vis, -1, sizeof(vis));
65     scanf("%d %d %d", &n, &m, &st);
66     for(int i = 0; i < m; ++i)
67     {
68         int x, y, w;
69         scanf("%d %d %d", &x, &y, &w);
70         G[x].push_back(node{y, w, 0});
71         G[y].push_back(node{x, w, 1});
72     }
73     vis[1] = st;
74     bfs();
75     for(int i = 1; i <= n; ++i)
76     {
77         if(vis[i] == -1)
78         {
79             rec = 1;
80             break;
81         }
82     }
83     if(rec)
84     {
85         cout << "QAQ";
86     }
87     else
88     {
89         cout << "QWQ" << endl;
90         for(int i = 1; i <= n; ++i)
91         {
92             cout << vis[i] << endl;
93         }
94     }
95     return 0;
96 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!