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

提示

 

来源

辽宁省赛2010

题目描述:

一个城市有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的更多相关文章

  1. SCNU 2015ACM新生赛初赛【1006. 3D打印】解题报告

            题目链接详见SCNU 2015新生网络赛 1006. 3D打印 .出题思路来自codevs 3288. 积木大赛,属于模拟题.         首先我们把“选择从第L部分到第R部分”理 ...

  2. PKU 1006

    数学问题吧,有兴趣的可以研究一下“中国剩余定理” // 1006.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include < ...

  3. 【BZOJ】1006: [HNOI2008]神奇的国度

    http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意:在一个弦图中找最少染色数.(n<=10000, m<=1000000) #i ...

  4. BZOJ 1006 [HNOI2008] 神奇的国度(简单弦图的染色)

    题目大意 K 国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即 AB 相互认识,BC 相互认识,CA 相互认识,是简洁高效的.为了巩固三角关系,K 国禁止四边关系,五边关系等 ...

  5. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  6. 1006 最长公共子序列Lcs

    1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...

  7. PAT乙级 1006. 换个格式输出整数 (15)

    1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们用字母B来表示“百” ...

  8. BZOJ 1006 神奇的国度(弦图的染色数)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1006 题意:给定一个弦图,求最小染色数.就是用最小数目的颜色进行染色使得任意两个相邻的节 ...

  9. NEU校园网登录器

    http://www.cnblogs.com/weidiao/p/5124106.html 改自学长的博客. 我们的目标是写一个程序实现自动登录校园网.而这基于的是表单的post机制. 输入校园网网址 ...

随机推荐

  1. guice基本使用,guice整合guice-servlet,web scope注解(六)

    guice servlet提供了几个比较有用的web scope,类似与传统servlet 的session,request这些提供的范围等. guice servlet 提供的web scope 如 ...

  2. Npgsql使用入门(一)【搭建环境】

    首先去官网下载最新数据库安装包 postgresql-9.6.1-1-windows-x64 将postgreSQL9.6注册为windows服务 注意:大小写要正确 D:\Worksoftware\ ...

  3. JavaScript获取非行间样式

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  4. 5) 十分钟学会android--ActionBar知识串烧

    建立ActionBar Action bar 最基本的形式,就是为 Activity 显示标题,并且在标题左边显示一个 app icon.即使在这样简单的形式下,action bar对于所有的 act ...

  5. 组装自己的tesla超级计算机

    原文链接:blog.csdn.net/xqj198404/article/details/20016279 NVIDIA链接:http://www.nvidia.cn/object/tesla_bui ...

  6. 如何用npm安装vue

    下载安装node.js,安装完毕做好配置后开始运行. 如果npm版本太低,需要升级一下npm # 查看版本 $ npm -v #升级 npm cnpm install npm -g 安装vue-cli ...

  7. angular 常用写法

    1.ng-repeat 数组数据中,不允许数组中有相同的两个数据,这个时候用下标去管理数据便可以解决这个问题 ng-repeat="item in list track by $index& ...

  8. Apex语言(一)开发环境

    1.注册salesforce开发者https://developer.salesforce.com/ 2.开发者登录https://login.salesforce.com/ 3.Apex开发者工具 ...

  9. 转载:轻量级浏览器特性检测库:feature.js

    feature.js是一个很简单.快速和轻量级的浏览器特性检测库,它没有任何依赖,体积压缩最后只有1KB,它可以自动初始化,在你需要知道某个特性是否可用时,直接引入即可.以下中文为个人理解. /*! ...

  10. obj-c部分对象快捷赋值和取值

    NSNumber: NSNumber *number = @1234; 旧的方式: NSArray *physicsValues = [NSArrayarrayWithObjects: [NSNumb ...