2015山东信息学夏令营 Day4T3 生产
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 生产的更多相关文章
- 2015山东信息学夏令营 Day5T3 路径
问题描述: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入: 第一行包含一个正整数n,表示点数. 接下来 ...
- [GRYZ]寒假模拟赛
写在前面 这是首次广饶一中的OIERS自编自导,自出自做(zuo)的模拟赛. 鉴于水平气压比较低,机(wei)智(suo)的WMY/XYD/HYXZC就上网FQ下海找了不少水(fei)题,经过他们优( ...
- POJ2286 The Rotation Game
Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...
- 小爬虫。爬取网站多页的通知标题并存取在txt文档里。
爬取网页中通知标题的内容展示: this is 1 page!<精算学综合>科目考试参考大纲2016年上半年研究生开题报告评议审核结果公示[答辩]2016下半年研究生论文答辩及学位评定 ...
- ARM9/ARM11/Cortex A8处理器(转载) .
//toppic:推荐几个值得中小企业使用的ARM9/ARM11/Cortex A8处理器 // 作者:gooogleman //原文地址:http://blog.csdn.net/goooglema ...
- Protobuf协议应用干货
Protobuf应用广泛,尤其作为网络通讯协议最为普遍.本文将详细描述几个让人眼前一亮的protobuf协议设计,对准备应用或已经应用protobuf的开发者会有所启发,甚至可以直接拿过去用. 这里描 ...
- 解析Visual Studio 2015促进生产力的10个新功能
1 性能提示 Performance Tips 当我们想知道执行一段代码所耗费的时间时,需要借助于.NET 框架的Stopwatch类,像下面这样: class Program { static vo ...
- 清橙A1363. 水位 - 清华大学2012年信息学优秀高中学子夏令营
问题描述 有一个正方形的地区,该地区特点鲜明:如果把它等分为N×N个小正方形格子的话,在每个格子内的任意地点的地表高度是相同的,并且是一个0到M之间的整数.正方形地区的外部被无限高的边界包围. 该地区 ...
- 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毛都不会.. ...
随机推荐
- redis 远程连接方法
解决方法 1.修改redis服务器的配置文件 vi redis.conf 注释以下绑定的主机地址 # bind 127.0.0.1 或 vim redis.conf bind 0.0.0.0 pr ...
- SpringIOC学习_属性注入(依赖注入)
一.应用场景:Spring会帮创建实现类的实例,但是有时候我们还需要在类中设置一些属性用于传入设置值,这些跟类紧密关联的属性就叫依赖,通过spring帮忙设置的过程叫依赖注入. 二.依赖注入的实现 A ...
- 2017团体程序设计天梯赛大区赛 L3-3 球队“食物链”
思路: 状压dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- vue cli 3 打包过大问题
vue cli 3 打包命令 npm run build,这种情况下的打包可以通过设置 vue.config.js里面的 productionSourceMap: false. 如果是自己设置的打包环 ...
- 移动端rem
手机有很多尺寸的型号.使用rem来做为大小单位可以达到兼容的目的. 方法一:js测量手机尺寸,设置font-size:为手机屏幕width /10 + ‘px’.即10rem 为手机屏幕width. ...
- ibatis 的sqlMap 的xml 关注点
1.当有特殊字符时候需要保持原状 eg:特殊字符 <> 错误:t.name<>' ' 会报The content of elements must consist of ...
- C#枚举中的位运算权限分配
什么是位运算 常用的位运算主要有与(&), 或(|)和非(~), 比如: & = ; | = ; ~ = ; 运用在权限设计中 先建立一个枚举表示所有的权限管理操作: [Flags] ...
- ES6语法糖集锦
sublime3安装Es6插件 javascriptNext,然后安装即可 JavaScriptNext - ES6 Syntax()高亮插件 -------------------------- ...
- CentOS 6.4 linux下编译安装MySQL5.6.14
CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm ...
- swift class type isa-swizzling
class 是引用类型,生成的实例分布在 Heap(堆) 内存区域上,在 Stack(栈)只存放着一个指向堆中实例的指针.因为考虑到引用类型的动态性和 ARC 的原因,class 类型实例需要有一块单 ...