P1073 最优贸易 建立分层图 + spfa
作者:互联网
P1073 最优贸易:https://www.luogu.org/problemnew/show/P1073
题意:
有n个城市,每个城市对A商品有不同的定价,问从1号城市走到n号城市可以最多赚多少差价。(旅游为主,赚钱为辅,所以买入和卖出只进行一次。
思路:
建一个有三层的图,三层都是相同的普通的城市路线,第一层向第二层连从第i个城市买入商品的花费,第二层向第三层连从第i个城市卖出商品的所得。从1 向 第一层的终点 ,向第三层的终点跑一遍最大路就行了。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1000000007; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 1e5+9; vector<pii>mp[maxn*3]; int a[maxn]; int n,m; void addedge(int s,int t){ mp[s].pb(pii(t, 0)); mp[s + n].pb(pii(t + n,0)); mp[s + 2 * n].pb(pii(t + 2 * n,0)); mp[s].pb(pii(t + n, -a[s])); mp[s + n].pb(pii(t + 2 * n, a[s])); } int dis[maxn * 3]; bool vis[maxn * 3]; int spfa(int s,int t){ memset(dis, -inf,sizeof(dis)); dis[s] = 0; queue<int>que; que.push(s); vis[s] = true; while(!que.empty()){ int u = que.front(); que.pop(); vis[u] = false; for(int i=0; i<mp[u].size(); i++){ int v = mp[u][i].fi,w = mp[u][i].se; if(dis[v] < dis[u] + w){ dis[v] = dis[u] + w; if(vis[v] == false){ vis[v] = true; que.push(v); } } } } return dis[t]; } int main(){ scanf("%d%d", &n, &m); int t = 3 * n + 1; for(int i=1; i<=n; i++) scanf("%d", &a[i]); for(int i=1; i<=m; i++){ int u,v,c; scanf("%d%d%d", &u, &v, &c); if(c == 1) { addedge(u,v); } else { addedge(u,v); addedge(v,u); } } // addedge(3*n, t); mp[3*n].pb(pii(t,0)); mp[n].pb(pii(t, 0)); printf("%d\n", spfa(1, t)); return 0; }View Code
标签:pii,ch,int,分层,spfa,que,include,P1073,define 来源: https://www.cnblogs.com/ckxkexing/p/10351183.html