2015山东信息学夏令营 Day4T3 生产

【题目描述】

工厂为了生产一种复杂的产品,给各个生产部门制定了详细的生产计划。那么,就经常会有生产部门要把产品送到另一个生产部门作为原料。这是一个注重产品质量的工厂,所以每当有产品要从A部门运到B部门时,都要先从A部门送到质量检验处,检验合格后再从质量检验处运到B部门。

有些部门之间有传送带连接,厂长想知道每次将产品从一个部门运送到另一个部门最少需要多长时间。

【输入格式】

第一行两个整数n、m,n表示部门数量,m表示传送带数量。出于方便,1号部门是质量检验处。

接下来m行,每行三个整数u、v、w,表示有一条从u部门到v部门的传送带,传送过去需要w个单位时间。注意传送带是单向的。

接下来一个整数q,表示有q次运送。

接下来q行,每行两个数a、b,表示这一次要将产品从a部门运送到b部门。

【输出格式】

输出q行,每行一个整数,表示这次运送最少需要的时间。若没有传送方案,输出-1。

【样例输入】

5 5

1 2 3

1 3 5

4 1 7

5 4 1

5 3 1

3

4 2

5 3

2 3

【样例输出】

10

13

-1

【数据规模与约定】

30%的数据,n≤100,m≤500,w=1

60%的数据,n≤100,m≤5000

另20%的数据,q=1

100%的数据,2≤n≤3000,m≤100000,2≤a,b≤n,

q≤100000,1≤u,v≤n,1≤w≤10000

有些部门之间可能有多条传送带。

工厂的员工都非常尽职尽责,他们的认真和热情决定了产品的完美,所以不必考虑产品不合格的情况。

思路:

1、最短路,A到1再到B的距离,等于1到B的距离加上1到A的距离

2、存者正反两个图,在反图上求1到A距离,在正图上求1到B的距离

代码:

①官方标程(spfa + 链式前向星)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
const int M = ;
const int inf = ;
int n,m,q;
struct Edge
{
int u,v,w;
}xu[M];
int point[N],to[M],next[M],val[M];
int dui[N],mina[N],minb[N],top,tail;
bool indui[N];
void MakeMinLen(int x[])
{
int i;
dui[]=;
top=;tail=;
indui[]=;
for(i=;i<=n;i++)
x[i]=inf;
x[]=;
while(top^tail)
{
top++;
if(top==N)
top=;
int now=dui[top];
int then=point[now];
indui[now]=;
while(then)
{
int tox=to[then];
if(x[tox]>x[now]+val[then])
{
x[tox]=x[now]+val[then];
if(!indui[tox])
{
indui[tox]=;
tail++;
if(tail==N)
tail=;
dui[tail]=tox;
}
}
then=next[then];
}
}
}
void InitGraph()
{
int i;
scanf("%d%d",&n,&m);
for(i=;i<=m;i++)
scanf("%d%d%d",&xu[i].u,&xu[i].v,&xu[i].w);
memset(point,,sizeof point);
for(i=;i<=m;i++)
{
next[i]=point[xu[i].v];
point[xu[i].v]=i;
to[i]=xu[i].u;
val[i]=xu[i].w;
}
MakeMinLen(mina);
memset(point,,sizeof point);
for(i=;i<=m;i++)
{
next[i]=point[xu[i].u];
point[xu[i].u]=i;
to[i]=xu[i].v;
val[i]=xu[i].w;
}
MakeMinLen(minb);
}
void MakeAns()
{
scanf("%d",&q);
while(q--)
{
int a,b;
scanf("%d%d",&a,&b);
int res=mina[a]+minb[b];
if(res>=inf)
res=-;
printf("%d\n",res);
}
}
int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
InitGraph();
MakeAns();
return ;
}

②自己写的,感觉dij快一点,一测发现比标程慢不少TAT

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define inf ~0U>>2
#define maxn 3005
using namespace std;
struct orz{
int d;
int p;
friend bool operator < (orz a,orz b){
return a.d > b.d;
}
};
struct edge{
int v;
int w;
};
priority_queue< orz > ss;
vector<edge> g[maxn],op[maxn];
int n,m,q,flag = ,v[maxn],d[maxn],opd[maxn];
void init(){
cin>>n>>m;
int u,v,w;
edge tmp;
for(int i = ;i <= m;i++){
scanf("%d%d%d",&u,&v,&w);
tmp.v = v;
tmp.w = w;
g[u].push_back(tmp);
tmp.v = u;
op[v].push_back(tmp);
}
cin>>q;
for(int i = ;i <= n;i++) d[i] = opd[i] = inf;
}
void dij(){
orz tmp;
tmp.d = ;
tmp.p = ;
opd[] = ;
ss.push(tmp);
flag++;
int x,dd,to,wei;
edge j;
while(!ss.empty()){
tmp = ss.top();
ss.pop();
x = tmp.p;
dd = tmp.d;
if(v[x] == flag) continue;
v[x] = flag;
for(int i = ;i < g[x].size();i++){
j = g[x][i];
to = j.v;
wei = j.w;
if(d[to] > dd + wei){
d[to] = dd + wei;
tmp.d = dd + wei;
tmp.p = to;
ss.push(tmp);
}
}
}
}
void opdij(){
orz tmp;
tmp.d = ;
tmp.p = ;
opd[] = ;
ss.push(tmp);
flag++;
int x,dd,to,wei;
edge j;
while(!ss.empty()){
tmp = ss.top();
ss.pop();
x = tmp.p;
dd = tmp.d;
if(v[x] == flag) continue;
v[x] = flag;
for(int i = ;i < op[x].size();i++){
j = op[x][i];
to = j.v;
wei = j.w;
if(opd[to] > dd + wei){
opd[to] = dd + wei;
tmp.d = dd + wei;
tmp.p = to;
ss.push(tmp);
}
}
}
}
void ask(){
int u,v;
long long dn;
for(int i = ;i <= q;i++){
scanf("%d%d",&u,&v);
dn = opd[u] + d[v];
if(dn >= inf) cout<<-<<endl;
else cout<<dn<<endl;
}
}
int main(){
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
init();
dij();
opdij();
ask();
return ;
}

2015山东信息学夏令营 Day4T3 生产的更多相关文章

  1. 2015山东信息学夏令营 Day5T3 路径

    问题描述: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入: 第一行包含一个正整数n,表示点数. 接下来 ...

  2. [GRYZ]寒假模拟赛

    写在前面 这是首次广饶一中的OIERS自编自导,自出自做(zuo)的模拟赛. 鉴于水平气压比较低,机(wei)智(suo)的WMY/XYD/HYXZC就上网FQ下海找了不少水(fei)题,经过他们优( ...

  3. POJ2286 The Rotation Game

    Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...

  4. 小爬虫。爬取网站多页的通知标题并存取在txt文档里。

    爬取网页中通知标题的内容展示: this is  1  page!<精算学综合>科目考试参考大纲2016年上半年研究生开题报告评议审核结果公示[答辩]2016下半年研究生论文答辩及学位评定 ...

  5. ARM9/ARM11/Cortex A8处理器(转载) .

    //toppic:推荐几个值得中小企业使用的ARM9/ARM11/Cortex A8处理器 // 作者:gooogleman //原文地址:http://blog.csdn.net/goooglema ...

  6. Protobuf协议应用干货

    Protobuf应用广泛,尤其作为网络通讯协议最为普遍.本文将详细描述几个让人眼前一亮的protobuf协议设计,对准备应用或已经应用protobuf的开发者会有所启发,甚至可以直接拿过去用. 这里描 ...

  7. 解析Visual Studio 2015促进生产力的10个新功能

    1 性能提示 Performance Tips 当我们想知道执行一段代码所耗费的时间时,需要借助于.NET 框架的Stopwatch类,像下面这样: class Program { static vo ...

  8. 清橙A1363. 水位 - 清华大学2012年信息学优秀高中学子夏令营

    问题描述 有一个正方形的地区,该地区特点鲜明:如果把它等分为N×N个小正方形格子的话,在每个格子内的任意地点的地表高度是相同的,并且是一个0到M之间的整数.正方形地区的外部被无限高的边界包围. 该地区 ...

  9. 2015北大夏令营day1 B:An Idea of Mr. A

    题意:给定一个范围l,r计算i,j(i<j)属于这个范围内的gcd(2^(2^i)+1,2^(2^j)+1)的总和. 思路:费马数的应用,让我惊呆的是当年居然有123个人会做,我tm毛都不会.. ...

随机推荐

  1. 463 Island Perimeter 岛屿的周长

    详见:https://leetcode.com/problems/island-perimeter/description/ C++: class Solution { public: int isl ...

  2. apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)(非HA和HA)

    不多说,直接上干货! Storm的版本选取 我这里,是选用apache-storm-1.0.2.tar.gz apache-storm-0.9.6.tar.gz的集群搭建(3节点)(图文详解) 为什么 ...

  3. Oracle的一些名词和概念

    1.数据库 这里的数据库不是通常情况下我们所说的数据库,而是一个Oracle的专业名词.它是磁盘上存储数据的集合,在物理上表现为数据文件. 日志文件和控制文件等,在逻辑上以表空间形式存在.使用时,必须 ...

  4. laravel5.5文件上传

    /**     * 上传文件     * @param Request $request     * @return array     */    public function upload(Re ...

  5. Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解

    只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...

  6. robotframework + python2.7.9 + selenium 2.44.0 + selenium2library1.7 测试环境搭建成功!

    真心不容易呀!开源软件搭建挺麻烦的,各种组件未必要使用最新的版本:有些最新版本反而不兼容.需要仔细看官方说明书来进行搭建(官方网站都是英文),所以闹得重新安装了几次. 先上测试用例通过的图:

  7. js添加千位分隔符

    function thousandBitSeparator(num){ var re=/\d{1,3}(?=(\d{3})+$)/g; var n1=num.toString().replace(/^ ...

  8. 阿里云ECS安装sqlserver,本地无法连接问题排查思路

    1. 阿里云控制台-对应的ECS实例的安全组是否添加了响应的端口(1433)可以访问: 2. 服务器-sqlserver服务是否开启: 3. 服务器-sqlserver配置器,对应的端口是否启用,已经 ...

  9. (转) 淘淘商城系列——使用FastDFS-Client客户端进行上传图片的测试

    http://blog.csdn.net/yerenyuan_pku/article/details/72804018 不久之前,我们实现了商品的类目选择这个功能,但这只是万里长征的第一步,我们还有很 ...

  10. ansible配置mysql主从复制

    配置主机1.下载安装所需安装包 [root@server1 ansible]# lsansible-2.7.8-1.el7.noarch.rpmansible-tower-setup-bundle-3 ...