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机制. 输入校园网网址 ...
随机推荐
- Hadoop MapReduce编程 API入门系列之最短路径(十五)
不多说,直接上代码. ======================================= Iteration: 1= Input path: out/shortestpath/input. ...
- javascript 的逻辑中断(短路操作)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Object的wait和Thread的sleep
Object的wait() wait()搭配notify(),nofityAll()使用. 线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notif ...
- [ Database ] [ SQL Server ] SQL Server 很多不允許的操作解決方法
說明可參考 https://blog.miniasp.com/post/2012/10/26/SQL-Server-Management-Studio-Prevent-saving-changes-t ...
- vue-cli 2.0 常用命令
一.查询npm版本 npm -v 二.安装npm npm install npm g 三.安装webpack npm install webpack -g 四.安装vue命令行工具 npm insta ...
- JQuery学习笔记系列(一)----选择器详解
笔者好长时间没有更新过博客园的笔记了,一部分原因是去年刚刚开始工作一段时间忙碌的加班,体会了一种每天加班到凌晨的充实感,之后闲暇时间了也因为自己懒惰没有坚持记笔记的习惯,现在重新拾起来. 借用古人的一 ...
- java控制台输入输出字符串
一.实例说明 本实例通过输入流(System.in)实现从控制台接受用户输入信息,并将该信息输出到控制台. 运行效果如下图: 二.实现代码 三.要点说明 该实例的关键就是用到了System类的输入流, ...
- Memory management in RxSwift – DisposeBag
I’ve noticed a lot of beginners in RxSwift ask about DisposeBag. DisposeBag isn’t a standard thing i ...
- Rx (Reactive Extensions)
The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using ...
- Kaggle竞赛顶尖选手经验汇总
What is your first plan of action when working on a new competition? 理解竞赛,数据,评价标准. 建立交叉验证集. 制定.更新计划. ...