noip2013货车运输
来源:互联网 发布:淘宝卖家新手全攻略 编辑:程序博客网 时间:2024/04/28 12:22
货车运输
(truck.cpp/c/pas)
【问题描述】
A 国有n座城市,编号从1到n,城市之间有 m条双向道路。每一条道路对车辆都有重
量限制,简称限重。现在有 q辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的
情况下,最多能运多重的货物。
【输入】
输入文件名为truck.in。
输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道
路。
接下来m行每行3个整数x、y、z,每两个整数之间用一个空格隔开,表示从x 号城市
到y号城市有一条限重为z的道路。注意:x不等于y,两座城市之间可能有多条道路。
接下来一行有一个整数 q,表示有q辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市
运输货物到y城市,注意:x不等于y。
【输出】
输出文件名为truck.out。
输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn=10001,inf=1<<30;bool vis[maxn];int head[maxn],d[maxn][20],deep[maxn],fa[maxn][20];int n,m;int cnt;int father[maxn];struct ege{ int from,to,cost;}g[maxn];struct edge{ int to,next,v;}e[maxn];bool comp(ege x,ege y){ return x.cost>y.cost;}int find(int x){ if (father[x]==x) return x; return father[x]=find(father[x]);}void unite(int x,int y){ int fx=find(x),fy=find(y); if(fx!=fy) father[fx]=fy;}void adde(int u,int v,int w){ e[++cnt].to=v; e[cnt].v=w; e[cnt].next=head[u]; head[u]=cnt;}void insert(int u,int v,int w){ adde(u,v,w); adde(v,u,w);}void dfs(int x){ vis[x]=1; for(int i=1;i<=16;i++){ if(deep[x]<(1<<i)) break; fa[x][i]=fa[fa[x][i-1]][i-1]; d[x][i]=min(d[x][i-1],d[fa[x][i-1]][i-1]); } for(int i=head[x];i>0;i=e[i].next){ if(vis[e[i].to]==true) continue; fa[e[i].to][0]=x; d[e[i].to][0]=e[i].v; deep[e[i].to]=deep[x]+1; dfs(e[i].to); }}int lca(int x,int y){ if(deep[x]<deep[y]) swap(x,y); int t=deep[x]-deep[y]; for(int i=0;i<=16;i++){ if((1<<i)&t) x=fa[x][i]; } for(int i=0;i<=16;i++){ if(fa[x][i]!=fa[y][i]){ x=fa[x][i]; y=fa[y][i]; } } if(x==y) return x; return fa[x][0];}int ask(int x,int f){ int mn=inf; int t=deep[x]-deep[f];//printf("t==%d\n",t);/*for(int i=0;i<=16;i++) { if(t&(1<<i)) { mn=min(mn,d[x][i]); x=fa[x][i]; } }*/ for(int i=0;i<=16;i++){ if(t&(1<<i)){ mn=min(mn,d[x][i]); x=fa[x][i]; } } return mn;}int main(){ memset(d,127/3,sizeof(d)); freopen("truck.in","r",stdin); freopen("truck.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&g[i].from,&g[i].to,&g[i].cost); } sort(g+1,g+m+1,comp); for(int i=1;i<=n;i++) father[i]=i; int tot=0; for(int i=1;i<=m;i++){ int from=g[i].from,to=g[i].to,cost=g[i].cost; int p=find(from),q=find(to); if(p!=q) { unite(p,q); tot++; insert(from,to,cost); if(tot==n-1) break; } } for(int i=1;i<=n;i++) if(vis[i]==false) dfs(i); int q; scanf("%d",&q); //printf("niha%d\n",ask(3,1)); for(int i=1;i<=q;i++){ int x,y; scanf("%d%d",&x,&y); if(find(x)!=find(y)) { printf("-1\n"); continue; } else{ int t=lca(x,y); printf("%d\n",min(ask(x,t),ask(y,t))); } } return 0;}
0 0
- NOIP2013 货车运输
- NOIP2013货车运输 题解
- 【NOIP2013-D1T3】货车运输
- NOIP2013 货车运输
- [NOIP2013]货车运输
- [NOIp2013] 货车运输
- NOIP2013 货车运输
- NOIP2013 D1T3 货车运输
- 【NOIP2013】 货车运输
- NOIP2013 货车运输
- noip2013 货车运输
- codevs3287: [NOIP2013]货车运输
- noip2013货车运输
- NOIP2013货车运输
- noip2013货车运输
- NOIP2013货车运输
- 【NOIP2013】Day1T3 货车运输
- 【NOIP2013】货车运输
- Centreon+Nagios实战第五篇——监控端安装Centreon
- ajax异步交互
- JavaScript-事件绑定
- 解读 Linux 性能指标
- linux文本命令&用户和组命令 练习
- noip2013货车运输
- PopupMenu菜单以及开发工作中像素的计算
- APUE学习笔记5——第七章——进程环境
- hadoop2.5.2HDFS HA搭建
- GitHub error “Failed to get HEAD”
- 树莓派:django,uwsgi,nginx安装与设置
- poj2931 Procrastination
- adnroid 单元测试 简单实现
- 台阶问题