Time limit 1000 ms

Memory limit 131072 kB

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?

Input

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.

Output

For each test case, print one integer on a single line, giving the minimum cost. If it is impossible, print -1.

Sample Input

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

Sample Output

3
9 自己还没懂,记了网上大牛的代码。。就是这样 http://blog.csdn.net/ttl_135678942570/article/details/8068149
 #include<cmath>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<numeric>
#include<vector>
#include<ctime>
#include<queue>
#include<list>
#include<map>
#define pi acos(-1)
#define INF 0x7fffffff
#define clr(x) memset(x,0,sizeof(x));
#define clrto(x,siz,y) for(int xx=0;xx<=siz;xx++) x[xx]=y;
#define clrset(x,siz) for(int xx=0;xx<=siz;xx++) x[xx]=xx;
#define clrvec(x,siz) for(int xx=0;x<=siz;xx++) x[xx].clear();
#define fop freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define myprogram By_135678942570
#define clrcpy(x,siz,y) for(int xx=0;xx<siz;xx++) x[xx]=y[xx];
using namespace std;
struct node
{
int val;
int emp;
int to;
};
vector<node>road[];
int e[]={};
int f[]={};
int use[]={};
int vis[]={};
int n,m,q;
long long minn=INF;
void find(int pos,int sum)
{
if(sum>=minn)
return;
if(pos==n-)
{
minn=sum;
return;
}
for(int i=;i<road[pos].size();i++)
{
int next=road[pos][i].to;
if(!vis[next])
{
vis[next]=;
int k=road[pos][i].emp;
int c=road[pos][i].val;
use[k]++;
if(use[k]==)
find(next,sum+c);
else if(use[k]==)
find(next,sum+c+e[k]);
else find(next,sum+c+f[k]);
vis[next]=;
use[k]--;
}
}
}
main()
{
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
clr(vis);
clr(use);
for(int i=;i<=;i++)
road[i].clear();
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++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
node temp;
temp.to=b;
temp.val=d;
temp.emp=c;
road[a].push_back(temp);
}
minn=INF;
find(,);
if(minn==INF)
puts("-1");
else printf("%d\n",minn);
}
return ;
}

NBUT 1221 Intermediary 2010辽宁省赛的更多相关文章

  1. NBUT 1220 SPY 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB The National Intelligence Council of X Nation receives a ...

  2. NBUT 1219 Time 2010辽宁省赛

    Time limit   1000 ms Memory limit   131072 kB Digital clock use 4 digits to express time, each digit ...

  3. NBUT 1217 Dinner 2010辽宁省赛

    Time limit  1000 ms Memory limit  32768 kB Little A is one member of ACM team. He had just won the g ...

  4. NBUT 1224 Happiness Hotel 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB The life of Little A is good, and, he managed to get enoug ...

  5. NBUT 1222 English Game 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB This English game is a simple English words connection gam ...

  6. NBUT 1225 NEW RDSP MODE I 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB Little A has became fascinated with the game Dota recent ...

  7. NBUT 1218 You are my brother 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB Little A gets to know a new friend, Little B, recently. On ...

  8. NBUT 1223 Friends number 2010辽宁省赛

    Time limit  1000 ms Memory limit   131072 kB Paula and Tai are couple. There are many stories betwee ...

  9. ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21204 ...

随机推荐

  1. iBatis的基本配置+CRUD操作

    首先解释一下CRUD的含义:CRUD是指在做计算处理时的增加(Create).查询(Retrieve)(重新得到数据).更新(Update)和删除(Delete) 基本的数据库操作 创建工程iBati ...

  2. 机器学习 MLIA学习笔记(三)之 KNN(二) Dating可能性实例

    这是个KNN算法的另一实例,计算Dating的可能性. import numpy as np import os import operator import matplotlib import ma ...

  3. redis教程(The little redis book中文版)

    许可证 <The Little Redis Book>是经由Attribution-NonCommercial 3.0 Unported license许可的,你不需要为此书付钱. 你可以 ...

  4. MongoDB(课时5 数据查询)

    3.4.2 数据查询 对于数据的查询操作核心语法: db.集合名称.find({查询条件}, {设置显示的字段}) 范例:没查询条件 db.info.find() 范例:有查询条件,查询出url为&q ...

  5. scrapy 也能爬取妹子图?

    目录 前言 Media Pipeline 启用Media Pipeline 使用 ImgPipeline 抓取妹子图 瞎比比前言 我们在抓取数据的过程中,除了要抓取文本数据之外,当然也会有抓取图片的需 ...

  6. Linux中CentOS6.5 64位 系统下安装docker步骤

    CentOS6.5 64位    (docker目前仅支持64位)内核必须在3.10及以上 1. uname -r  查看内核版本 2. 升级内核到3.10版本(带aufs模块)    cd /etc ...

  7. 算法笔记--sg函数详解及其模板

    算法笔记 参考资料:https://wenku.baidu.com/view/25540742a8956bec0975e3a8.html sg函数大神详解:http://blog.csdn.net/l ...

  8. 关于angular5的惰性加载报错问题

    之前为了测试一个模块优化问题,于是用angular-cli快速搭建了个ng5的脚手架demo,在应用惰性加载功能的时候发现浏览器报错如下: ERROR Error: Uncaught (in prom ...

  9. 雷林鹏分享:C# 多态性

    C# 多态性 多态性意味着有多重形式.在面向对象编程范式中,多态性往往表现为"一个接口,多个功能". 多态性可以是静态的或动态的.在静态多态性中,函数的响应是在编译时发生的.在动态 ...

  10. English trip -- Review Unit4 Health 健康

    medicine    n. 药:医学:内科:巫术  vt. 用药物治疗:给…用药 drug  毒药;药店(drugstore) pill  药丸 patient 病人 head 头 hands 手 ...