vijosP1754 最优贸易
来源:互联网 发布:nginx虚拟主机是什么 编辑:程序博客网 时间:2024/04/29 08:23
双向bfs第一次求最短路,第二次求最长路,两次的值取差。
#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#include <vector>#include <queue>#include <iostream>using namespace std;#define maxn 110000#define pb(a) push_back(a);int getint(){ int res=0; char c; while(c=getchar(),c<'0'||c>'9'); res=c-'0'; while(c=getchar(),c>='0'&&c<='9') res=res*10+c-'0'; return res;}int n,m,pric[maxn],vis[maxn],val1[maxn],val2[maxn];vector<int>g1[maxn],g2[maxn];void bfs1(){ queue<int>q; memset(vis,0,sizeof(vis)); val1[1]=pric[1]; vis[1]=1; q.push(1); int now,next; while(!q.empty()) { now=q.front(); q.pop(); vis[now]=0; for(int i=0;i<g1[now].size();i++) { next=g1[now][i]; if(val1[next]>min(val1[now],pric[next])) { val1[next]=min(val1[now],pric[next]); if(!vis[next]) { vis[next]=1; q.push(next); } } } }}void bfs2(){ queue<int>q; memset(vis,0,sizeof(vis)); val2[n]=pric[n]; vis[n]=1; q.push(n); int now,next,tot; while(!q.empty()) { now=q.front(); q.pop(); vis[now]=0; for(int i=0;i<g2[now].size();i++) { next=g2[now][i]; if(val2[next]<max(val2[now],pric[next])) { val2[next]=max(val2[now],pric[next]); if(!vis[next]) { vis[next]=1; q.push(next); } } } }}int main(){ scanf("%d%d",&n,&m); rep(i,1,n) { pric[i]=getint(); } int a,b,c; rep(i,1,m) { a=getint(); b=getint(); c=getint(); if(c==1) { g1[a].pb(b); g2[b].pb(a); } else { g1[a].pb(b); g1[b].pb(a); g2[a].pb(b); g2[b].pb(a); } } memset(val2,0,sizeof(val2)); memset(val1,0x3f3f3f3f,sizeof(val1)); bfs1(); bfs2(); int ans=0; for(int i=1;i<=n;i++) { ans=max(ans,val2[i]-val1[i]); } printf("%d",ans); return 0;}
0 0
- vijosP1754 最优贸易
- 最优贸易
- 最优贸易
- 最优贸易
- 最优贸易
- 最优贸易
- trade 最优贸易
- VIJOS 1754 最优贸易
- 【Spfa】noip2009 最优贸易
- smartoj P1013 最优贸易
- 最优贸易解题报告
- [CODEVS 1173] 最优贸易
- [NOIP2009]最优贸易 T3
- vijos-1754 最优贸易
- [NOIP2009][spfa]最优贸易
- 【图论】NOIP2009最优贸易
- NOIP2009 最优贸易(BFS)
- NOIP2009 最优贸易
- 我的博客之路
- epoll原理与应用详解
- iOS-生成Bundle包-引入bundle-使用bundle
- 路由器的学习笔记
- 移动互联网时代必读十大图书
- vijosP1754 最优贸易
- unity3d保存自己的视图排版
- pat_b_1018 锤子剪刀布
- mySQL中replace的用法
- js 创建对象、修改对象、删除对象及引用对象案例
- Java关键字final、static使用总结
- 后台管理框架之七 :业务逻辑设计
- WinDbg解决BHO不加载
- Android 基于位置的开发与百度地图 (二)