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毛都不会.. ...
随机推荐
- PHP PDO事务处理及MYSQLengine=InnoDB
如果出现“#skip-innodb”则将“#”去掉,重启MySQL: 如果第一条无法解决,加上配置:default-storage-engine=InnoDB 再重启MySQL. 进入MYsql数据据 ...
- 把List<Map<String,Object>>转成Map<String,Object>
Map<String, Object> parmMap = new HashMap<String, Object>(); //定义一个用于存储强转后的Map List<M ...
- 用nowrap实现div内的元素不换行
在编写自定义插件my-slider的过程中,发现无论float还是inline-block均不能保证div内的内容不换行(这两个属性在内容超出容器尺寸后,均将剩余内容做换行处理),在浏览器内比较了自己 ...
- 10.3 Implementing pointers and objects and 10.4 Representing rooted trees
Algorithms 10.3 Implementing pointers and objects and 10.4 Representing rooted trees Allocating an ...
- spring.net应用
经过一段时间的调试,终于把spring.net中关于aop的方面给做个了一个比较完整的Demo.包含异常日志和性能日志.spring.net和log4net配置. http://files.cnblo ...
- fedora配置ip
fedora20配置静态ip 原创 2015年08月08日 14:36:01 标签: fedora / linux / 网络配置 / ip配置 / 网络设置 2403 在linux的世界里,给主机设置 ...
- CE工具里自带的学习工具--第四关
图解:
- dedecms部分文章出现读取附加信息出错的解决办法
问题: 估计是新版本开发的时候,没有考虑旧版,文章内容为空的新闻,新版不在允许文章内容为空的新闻,这样旧版的内容为空的新闻升级后将无法再编辑. 解决:你可以对如下文件进行如下修改:article_ed ...
- 04CSS文本字体及排版
CSS文本字体 字体——font-family font-family:字体1,字体2,字体3,……:应用font-family属性可以一次定义多个字体,而在浏览器读取字体时, 会按照定义的先后顺序来 ...
- 模板—trie图
做了某题之后发现trie的AC自动机太垃圾了,动不动就TLE,然后我就去学了trie图. #include<iostream> #include<cstdio> using n ...