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机制. 输入校园网网址 ...
随机推荐
- 多线程之HttpClient
在程序用调用 Http 接口.请求 http 资源.编写 http 爬虫等的时候都需要在程序集中进行 Http 请求. 很多人习惯的 WebClient.HttpWebRequest 在 TPL 下很 ...
- C# 时间日期(函数,解释)
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6- ...
- OnLineML:时序数据挖掘
关于时序分析: 我们跟随时间的脚步,试图解释现在.理解过去.甚至预测未来........ 原文链接:http://blog.sciencenet.cn/home.php?mod=space&u ...
- <td colspan="6"></td>代表这个td占6个td的位置
<td colspan="6"><span class="order-time">2017-10-11 14:55:23</spa ...
- Here comes Treble: A modular base for Android
On the Android team, we view each dessert release as an opportunity to make Android better for our u ...
- day28 re(正则)模块
目录 re模块有什么用? re模块的基础使用 元字符 终极 贪婪模式 非贪婪模式 re模块高级 comple match和search re.split() sub和subn 分组 re模块有什么用? ...
- ZJOI2015 幻想乡战略游戏 动态点分治_树链剖分_未调完
Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来, ...
- vim牛逼的code工具: ctags+ cscope
自己总结 在我的工作目录里的.vimrc中做了这样的配置: set tags=tags; set autochdir 在项目根目录里利用"sudo ctags -R *", ...
- 洛谷P1427 小鱼的数字游戏
题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了).这对小鱼的 ...
- 训练1-U
输入2个正整数A,B,求A与B的最小公倍数. Input 2个数A,B,中间用空格隔开.(1<= A,B <= 10^9) Output 输出A与B的最小公倍数. Sample Input ...