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. 【LeetCode】树的遍历

    非递归中序遍历: 思路:注释 vector<int> inorderTraversal(TreeNode* root) { vector<int>ret; if(root == ...

  2. [转]Monkey测试简介

    转自:http://www.cnblogs.com/manuosex/p/3215270.html 在android手机上做自动化测试,monkey比cts,Android UnitTest 好用多了 ...

  3. ZOJ 3605Find the Marble(dp)

    ZOJ 3605 大体意思就是 找出随机选了K个交换后 石子在第i个罐子里的概率最大 也就是可能的总数最大 这样就可以写出递推方程 dp[i][j][k] += dp[i-1][e][k]; (0&l ...

  4. Hadoop YARN学习之监控集群监控Nagios(4)

    doop YARN学习之监控集群监控Nagios(4) 1. Nagios是一个流行的开源监控工具,可以用来监控Hadoop集群. 2. 监控基本的Hadoop服务 调试好脚本后命名为chek_res ...

  5. Android 图片文件和Bitmap之间的转换

    String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath); 如果图片过大,可能导致 ...

  6. VS2015 update3 安装 asp.net core 失败

    CMD 命令下执行: C:\DotNetCore\DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1

  7. Harris角点检测原理详解

    http://blog.csdn.net/lwzkiller/article/details/54633670 关于角点的应用在图像处理上比较广泛,如图像匹配(FPM特征点匹配).相机标定等.网上也有 ...

  8. xcode 通配搜索

    class \w*<\w*> extension \w*: \w* \{\} 搜索所有泛型类.

  9. pycharm debug后会出现 step over /step into/step into my code /force step into /step out 分别表示

    1.debug,全部打印 2.打断点debug,出现单步调试等按钮,只运行断点前 3.setup over 调试一行代码 4.setup out 运行断点后面所有代码 5.debug窗口显示调试按钮 ...

  10. Linux命令学习(6):paste合并几列文件

    如果我们有三个文件: $ cat name.txt #姓名文档 Kevin Mary Tom $ cat gender.txt #性别文档 M F M $ cat age.txt #年龄文档 我们想把 ...