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机制. 输入校园网网址 ...
随机推荐
- X264编码实现
H264 H264的官方测试源码,由德国hhi研究所负责开发.特点:实现了264所有的特性,由于是官方的测试源码,所以学术研究的算法都是在JM基础上实现并和JM进行比较.但其程序结构冗长,只考虑引入各 ...
- javascript变量中基本类型和引用类型的详解解读
前言: Javascript语言中的变量和其他语言的变量有很大区别,javascript松散类型的本质,决定了它只是在特定时间时间保存特定值得名字而已.由于不存在定义某个变量必须保存何种数据类型值的规 ...
- 如何修改 WordPress 数据库前缀
我们知道 WordPress 的数据库表,可以设置前缀,默认是 wp_,很多同学也就默认用了 wp_,如果某种原因(比如提高安全性)要修改的 WordPress 数据的前缀,我们应该怎么做? 开始之前 ...
- 【Oracle】管理还原数据(undo)
1. 查看undo相关参数 SYS@LGR> show parameter undo NAME TYPE VALUE ------------------------------------ - ...
- 解决启动httpd报: apr_sockaddr_info_get() failed for错误
我测试库里 service httpd start 时报 下面错误 httpd: apr_sockaddr_info_get() failed for fengxin.wzjzt.centoshttp ...
- 数组、ArrayList、HashTable
相同点:都可以存储一组数据 不同点: 1)数组,必须要先分配空间,存储数据固定 2)ArrayList,存储数据可以自由扩展 3)HashTable与ArrayList一样,但是它是有意义的,键值对形 ...
- 转载:移动端自适应:flexible.js可伸缩布局使用
阿里团队开源的一个库.flexible.js,主要是实现在各种不同的移动端界面实现一稿搞定所有的设备兼容自适应问题. 实现方法: 通过JS来调整html的字体大小,而在页面中的制作稿则统一使用rem这 ...
- websocket 进阶!netty框架实现websocket达到高并发
引言: 在前面两篇文章中,我们对原生websocket进行了了解,且用demo来简单的讲解了其用法.但是在实际项目中,那样的用法是不可取的,理由是tomcat对高并发的支持不怎么好,特别是tomcat ...
- Aeroplane chess HDU - 4405_数学期望_逆推
Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...
- 前端异步编程之Promise和async的用法
传统的异步解决方案采用回调函数和事件监听的方式,而这里主要记录两种异步编程的新方案: ES6的新语法Promise ES2017引入的async函数 Generator函数(略) Promise的含义 ...