NEU 1006 Intermediary
1006: Intermediary
时间限制: 1 Sec 内存限制: 128 MB
提交: 261 解决: 25
[提交][状态][讨论版]
题目描述
It is widely known that any two strangers can get to know each other through at most six other people. Now let’s prove this.
In the country Intermediary Conducts Personal Communications (ICPC), there are up to n (2<=n<=100) ordinary people conveniently numbered from 0 to n-1. They don’t know each other, or, in other words, they are strangers. The only way they can communicate with each other is through the government, which, in fact, is an intermediary agency. The government consists of up to m (1<=m<=9) employees conveniently numbered from 0 to m-1. Suppose employee z can introduce person x to person y at a cost of d dollars. If this is the first time in a day that employee z introduce one person to another, he will only require d dollars. For the second time, he will require d dollars plus extra e dollars as his tip. For the third time and more, he will require d dollars plus extra f dollars. He is not dared to require any more than that since the strange country is somewhat democratic. And if person x is able to communicate with person t and person t is able to communicate with person y, then person t is always willing to transfer messages from person x to person y, at no charge. Of course, the intermediary fees are all paid by person x. Notice that employee z being able to introduce person x to person y doesn’t mean he can introduce person y to person x.
Now person 0 has to send a message to person n-1 in one day. If all employees have just started to work, what is the minimum cost for person 0?
输入
For each test case, the first line contains three integers, n, m and q, where q is the number of intermediary relationships and q is at most 10,000. The second line has m integers, each indicating the value e of every employee, in the range [0, 100]. The third line has m integers too, each indicating the value f of every employee, in the range [e, 200]. The next q lines each contains four integers, x, y, z and d, indicating that employee z can introduce person x to person y requiring d dollars, where 1<=d<=200. There is a blank line after each test case.
Proceed to the end of file.
输出
For each test case, print one integer on a single line, giving the minimum cost. If it is impossible, print -1.
样例输入
3 2 2
1 1
2 2
0 1 0 1
1 2 1 2 5 1 4
1
2
0 1 0 1
1 2 0 1
2 3 0 1
3 4 0 1
样例输出
3
9
提示
来源
题目描述:
一个城市有n个人编号从0到n-1,m个中介编号从0到m-1,大家互相都不认识,必须通过中介来传递信息,不同中介在不同人之间传递信息收费不同(单向的,比如0号中介可以把1号居民的信息传递给2号居民,反过来不一定成立),每个中介在第一次传递信息不收取小费,第二次收取小费e,第三次及其以上收取小费f(不同的中介收费不同)。
在这种情况下,0号居民想向n-1号居民传递一则信息,问最小花费多少。
输入:
多测试点,每个点的输入格式如下
第一行n,m,q,表示居民数,中介数,和中介可以达成的传递种数
第二行m个数代表每个中介的小费e
第三行m个数代表每个中介的小费f
第四行到第3+q行每行四个数字x,y,z,d,表示中介z可以传递居民x的信息给居民y,花费为d(没有加上小费)。
输出:
一行,一个数字,代表0号居民向n-1号居民传递一则信息的最小花费。
思路:暴力搜索加最优化剪枝
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int mid[];
int n,m,q;
int E[],F[];
int x,y,z,d;
int lin[],cnt;
struct str
{
int next,y,z,d;
}e[];
void insertt()
{
e[++cnt].y=y;
e[cnt].next=lin[x];
lin[x]=cnt;
e[cnt].z=z;
e[cnt].d=d;
}
int vis[];
int ans,temp;
void work(int x)
{
if(x==n-)
{
ans=ans>temp?temp:ans;
return ;
}
if(temp>=ans)return ;
for(int i=lin[x];i;i=e[i].next)
{
if(!vis[e[i].y])
{
vis[e[i].y]=;
temp+=e[i].d;
int tt=++mid[e[i].z];
if(tt==)temp+=E[e[i].z];
else if(tt>)temp+=F[e[i].z];
work(e[i].y);
if(tt==)temp-=E[e[i].z];
else if(tt>)temp-=F[e[i].z];
--mid[e[i].z];
vis[e[i].y]=;
temp-=e[i].d;
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
cnt=;
memset(e,,sizeof(e));
memset(lin,,sizeof(lin));
memset(mid,,sizeof(mid));
memset(vis,,sizeof(vis));
for(int i=;i<m;i++)
scanf("%d",&E[i]);
for(int i=;i<m;i++)
scanf("%d",&F[i]);
for(int i=;i<q;i++)
{
scanf("%d%d%d%d",&x,&y,&z,&d);
insertt();
}
ans=;
temp=;
work();
printf("%d\n",ans);
}
return ;
}
但是标称不是搜索,有点迷,不知道什么,先贴上来慢慢研究
#include<cstring> #include<cstdio> #define __FILE_GENERATOR__
struct Edge
{
int m,w,l;
Edge *next;
};
int const INF=0x3fffffff;
int const N=,M=,Q=;
int const S=; //开了hash
int p3[M+];
Edge edges[Q];
Edge *adj[N];
int e[M],f[M];
int ii[S],jj[S];
int d[S];
bool b[S];
int n,m,q;
int cnt,num;
inline void initialize() //初始化
{
int k;
p3[]=;
for(k=;k<=M;++k)
p3[k]=p3[k-]*; //设计的hash算法
}
inline Edge* makeEdge(int m,int w,int l,Edge *next) //链式前向星结构
{
edges[cnt].m=m; //m是中间人编号
edges[cnt].w=w; //w是 y
edges[cnt].l=l; //l是花费
edges[cnt].next=next;
return &edges[cnt];
}
bool readin()
{
int x,y,z,d,k;
if(scanf(\"%d%d%d\",&n,&m,&q)==EOF)
return false;
for(k=;k<m;++k)
scanf(\"%d\",&e[k]);
for(k=;k<m;++k)
scanf(\"%d\",&f[k]);
for(k=;k<n;++k)
adj[k]=NULL;
for(cnt=;cnt<q;++cnt)
{
scanf(\"%d%d%d%d\",&x,&y,&z,&d);
adj[x]=makeEdge(z,y,d,adj[x]);
}
return true;
}
inline int D(int index)
{
return d[index];
}
void remove()
{
int x,y,z,r=,pr,left,right;
z=ii[num];
ii[]=z;
jj[z]=;
--num;
while((r<<)<=num)
{
left=r<<;
right=(r<<)+;
if(right<=num)
pr=D(ii[left])<D(ii[right])?left:right;
else
pr=left;
if(D(ii[r])<=D(ii[pr]))
break;
x=ii[r];
y=ii[pr];
ii[r]=y;
jj[y]=r;
ii[pr]=x;
jj[x]=pr;
r=pr;
}
}
void adjust(int pr)
{
int x,y,r;
while((pr>>)>=)
{
r=pr>>;
if(D(ii[r])<=D(ii[pr]))
break;
x=ii[r];
y=ii[pr];
ii[r]=y;
jj[y]=r;
ii[pr]=x;
jj[x]=pr;
pr=r;
}
}
void solve()
{
int status,pstatus,nstatus,v,w,mid,t,l,k;
Edge *pE;
cnt=p3[m]*n;
for(k=;k<cnt;++k)
{
d[k]=INF;
ii[k+]=k;
jj[k]=k+;
}
memset(b,false,sizeof(b));
num=cnt;
d[]=;
while(true)
{
if(num==)
{
printf(\"-1\\n\");
return;
}
pstatus=ii[];
if(d[pstatus]==INF)
{
printf(\"-1\\n\");
return;
}
b[pstatus]=true;
v=pstatus/p3[m];
status=pstatus%p3[m];
if(v==n-)
{
printf(\"%d\\n\",d[pstatus]);
return;
}
pE=adj[v];
remove();
while(pE!=NULL)
{
mid=pE->m;
t=(status%p3[mid+])/p3[mid];
w=pE->w;
l=pE->l;
if(t==)
{
nstatus=status+p3[mid];
}
else if(t==)
{
l+=e[mid];
nstatus=status+p3[mid];
}
else if(t==)
{
l+=f[mid];
nstatus=status;
}
nstatus+=w*p3[m];
if(!b[nstatus]&&d[pstatus]+l<d[nstatus])
{
d[nstatus]=d[pstatus]+l;
adjust(jj[nstatus]);
}
pE=pE->next;
}
}
}
int main()
{
freopen(\"1006.in2\",\"r\",stdin);
initialize();
while(readin())
solve();
return ;
}
NEU 1006 Intermediary的更多相关文章
- SCNU 2015ACM新生赛初赛【1006. 3D打印】解题报告
题目链接详见SCNU 2015新生网络赛 1006. 3D打印 .出题思路来自codevs 3288. 积木大赛,属于模拟题. 首先我们把“选择从第L部分到第R部分”理 ...
- PKU 1006
数学问题吧,有兴趣的可以研究一下“中国剩余定理” // 1006.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...
- 【BZOJ】1006: [HNOI2008]神奇的国度
http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意:在一个弦图中找最少染色数.(n<=10000, m<=1000000) #i ...
- BZOJ 1006 [HNOI2008] 神奇的国度(简单弦图的染色)
题目大意 K 国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即 AB 相互认识,BC 相互认识,CA 相互认识,是简洁高效的.为了巩固三角关系,K 国禁止四边关系,五边关系等 ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- PAT乙级 1006. 换个格式输出整数 (15)
1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们用字母B来表示“百” ...
- BZOJ 1006 神奇的国度(弦图的染色数)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1006 题意:给定一个弦图,求最小染色数.就是用最小数目的颜色进行染色使得任意两个相邻的节 ...
- NEU校园网登录器
http://www.cnblogs.com/weidiao/p/5124106.html 改自学长的博客. 我们的目标是写一个程序实现自动登录校园网.而这基于的是表单的post机制. 输入校园网网址 ...
随机推荐
- 修改Visual Studio2010的主题颜色
第一步:打开工具->扩展管理器 第二步:搜素visual studio color theme editor 第三步:找到Visual Studio Color Theme Editor 第四步 ...
- Super超级ERP系统---(10)订单打包
订单拣货完成后,需要把订单装箱打包,并打印客户地址信息.订单打包的操作流程先是扫描订单号,然后扫描商品条码. 1.订单打包 打印包装箱面单 2.订单发货 订单打包完成后就等待发货,快递公司来拉货的时 ...
- 八叉树(Octree)Typescript 实现
Demo GitHub export class Octree { // 父&子树 private parent_node: any; private children_nodes: Octr ...
- how does Array.prototype.slice.call() work?
763 down vote accepted +50 What happens under the hood is that when .slice() is called normally, thi ...
- JavaScript Math 对象常用方法
Math.abs(x):可返回数的绝对值 Math.ceil(x):向上取整 Math.floor(x):向下取整 Math.max(x,y):最大值 Math.min(x,y):最小值 Math.r ...
- TF基础5
卷积神经网络CNN 卷积神经网络的权值共享的网络结构显著降低了模型的复杂度,减少了权值的数量. 神经网络的基本组成包括输入层.隐藏层和输出层. 卷积神经网络的特点在于隐藏层分为卷积层和池化层. pad ...
- Python之进程 进阶 下
在python程序中的进程操作 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起 ...
- Javase 简单练习
public class Test10 { public static void main(String[] args) { System.out.println("------------ ...
- Tab 切换效果
今天写的两个小效果都是为了测试我写的单参函数,结果还是有点成功的~~此处是不是想发表情包! Tab效果很简单,这里我就不赘述了,直接上代码: html代码: <div class="c ...
- UVA-11806 Cheerleaders 计数问题 容斥定理
题目链接:https://cn.vjudge.net/problem/UVA-11806 题意 在一个mn的矩形网格里放k个石子,问有多少方法. 每个格子只能放一个石头,每个石头都要放,且第一行.最后 ...