1 #include<iostream> 2 #include<string> 3 #include<queue> 4 #include<stack> 5 #include<vector> 6 #include<map> 7 #include<cstdio> 8 #include<cstdlib> 9 #include<algorithm> 10 #include<set> 11 #include<list> 12 #include<iomanip> 13 #include<cstring> 14 #include<cmath> 15 #include<limits> 16 using namespace std; 17 18 #define au auto 19 #define debug(i) cout<<"<debug> "<<i<<" <\debug>"<<endl 20 #define mfor(i,a,b) for(register int i=(a);i<=(b);i++) 21 #define mrep(i,a,b) for(register int i=(a);i>=(b);i--) 22 #define LLL __int128 23 #define Re register 24 #define il inline 25 #define mem(a,b) memset(a,(b),sizeof(a)) 26 typedef pair<int, int> intpair; 27 typedef pair<long long int, long long int> llpair; 28 typedef long long int LL; 29 const int INF = 0x3f3f3f3f; 30 const long long int INFLL = 0x3f3f3f3f3f3f3f3f; 31 32 int cnt; 33 int n, m; 34 const int maxn = 300010; 35 int v[maxn]; 36 37 struct Edge 38 { 39 int u, nxt; 40 int w; 41 }e[maxn * 3]; 42 43 int head[maxn * 3]; 44 45 void add(int a, int b, int w) 46 { 47 e[++cnt].u = b; 48 e[cnt].nxt = head[a]; 49 head[a] = cnt; 50 e[cnt].w = w; 51 } 52 53 int dis[maxn * 3]; 54 bool vis[maxn * 3]; 55 queue<int>q; 56 57 void spfa(int x) 58 { 59 mem(dis, -0x3f); 60 mem(vis, false); 61 dis[x] = 0; 62 q.push(x); 63 vis[x] = true; 64 while (!q.empty()) 65 { 66 int t = q.front(); 67 q.pop(); 68 vis[t] = false; 69 for (Re int i = head[t]; i != -1; i = e[i].nxt) 70 { 71 int u = e[i].u; 72 int w = e[i].w; 73 if (dis[u] < dis[t] + w) 74 { 75 dis[u] = dis[t] + w; 76 if (!vis[u]) 77 { 78 q.push(u); 79 vis[u] = true; 80 } 81 } 82 } 83 } 84 } 85 86 int main() 87 { 88 mem(head, -1); 89 cin >> n >> m; 90 mfor(i, 1, n) cin >> v[i]; 91 mfor(i, 1, m) 92 { 93 int a, b, c; 94 cin >> b >> c >> a; 95 add(b, c, 0); 96 add(n + b, n + c, 0); 97 add((n << 1) + b, (n << 1) + c, 0); 98 add(b, n + c, -v[b]); 99 add(n + b, (n << 1) + c, v[b]); 100 if (a == 2) 101 { 102 add(c, b, 0); 103 add(n + c, n + b, 0); 104 add((n << 1) + c, (n << 1) + b, 0); 105 add(c, n + b, -v[c]); 106 add(n + c, (n << 1) + b, v[c]); 107 } 108 } 109 add(n, 3 * n + 1, 0); 110 add(n * 3, n * 3 + 1, 0); 111 spfa(1); 112 cout << dis[n * 3 + 1]; 113 return 0; 114 }
原文地址:https://www.cnblogs.com/thjkhdf12/p/11766328.html
时间: 2024-11-13 07:58:28