【NOIP2009】最优贸易
2015-10-19 14:53
344 查看
描述
C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个
城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分
为双向通行的道路,双向通行的道路在统计条数时也计为 1 条。
C 国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价
格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。
商人阿龙来到 C 国旅游。当他得知同一种商品在不同城市的价格可能会不同这一信息
之后,便决定在旅游的同时,利用商品在不同城市中的差价赚回一点旅费。设 C 国 n 个城
市的标号从 1~ n,阿龙决定从 1 号城市出发,并最终在 n 号城市结束自己的旅行。在旅游的
过程中,任何城市可以重复经过多次,但不要求经过所有 n 个城市。阿龙通过这样的贸易方
式赚取旅费:他会选择一个经过的城市买入他最喜欢的商品——水晶球,并在之后经过的另
一个城市卖出这个水晶球,用赚取的差价当做旅费。由于阿龙主要是来 C 国旅游,他决定
这个贸易只进行最多一次,当然,在赚不到差价的情况下他就无需进行贸易。
假设 C 国有 5 个大城市,城市的编号和道路连接情况如下图,单向箭头表示这条道路
为单向通行,双向箭头表示这条道路为双向通行。
#include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> #include<vector> #include<algorithm> #include<queue> #include<cstring> #define maxn 1000000+5 #define inf 0x7fffffff #define xiao 1e-9 using namespace std; struct edge{int next,to;}g[maxn*5],lead[maxn*5]; int head[maxn],head2[maxn],low[maxn],high[maxn],price[maxn],cnt,n,m; inline void add(int u,int v) { ++cnt; g[cnt].to=v; g[cnt].next=head[u]; head[u]=cnt; lead[cnt].to=u; lead[cnt].next=head2[v]; head2[v]=cnt;} inline void spfa(){ queue<int> q; q.push(1); int nhead,tmp; memset(low,0x7f,sizeof(low)); while(!q.empty()){ nhead=q.front(); q.pop(); for(int i=head[nhead];i;i=g[i].next){ tmp=g[i].to; if(low[tmp]>min(low[nhead],price[tmp])) { low[tmp]=min(low[nhead],price[tmp]);q.push(tmp);} } } } inline void spfa1(){ queue<int> q; q.push(n); int nhead,tmp; while(!q.empty()) { nhead=q.front(); q.pop(); for(int i=head2[nhead];i;i=lead[i].next) { tmp=lead[i].to; if(high[tmp]<max(high[nhead],price[tmp])) { high[tmp]=max(high[nhead],price[tmp]); q.push(tmp);} } } } void work(){ int ans=0; for(int i=1;i<=n;++i) ans=max(high[i]-low[i],ans); printf("%d\n",ans); } void init() { int x,y,z; cin>>n>>m; for(int i=1;i<=n;++i) scanf("%d",&price[i]); for(int i=1;i<=m;++i) { scanf("%d%d%d",&x,&y,&z); if(z==1) add(x,y); else {add(x,y);add(y,x);} } } int main() { freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); init(); spfa(); spfa1(); work(); return 0; } /*5 5 4 3 5 6 1 1 2 1 1 4 1 2 3 2 3 5 1 4 5 2*/View Code
相关文章推荐
- php 抓取中国统计局 最新县及县以上行政区划代码
- codeforces-303-B. Equidistant String-水题
- Linux中如何去掉LCD上闪烁的光标
- 线性表---单循环链表(约瑟夫环问题)
- iOS9新特性之实现3D Touch就是So easy(更新Swift版)
- 用 OneAPM Cloud Insight 监控 Docker 性能
- android xmlns:tools用法
- Xutil过时出现的HttpException:java.io,IoException:stack size 1036KB的异常
- Github比较有趣的PHP项目
- ios block中引用self
- sql中用JOIN USING 简化JOIN ON
- android 去除title bar 标题栏 action bar
- ios layer的一些学习
- CAS实现单点登录(SSO)经典完整教程
- phpstorm9.02 下载
- JS控制文本框textarea输入字数限制的方法
- android测试参考
- Java Web开发之基于Session的购物商店实现方法
- redirect和forward
- codeforces#303-C. Woodcutters-贪心