NBUT 1221 Intermediary 2010辽宁省赛

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

 1 #include<cmath>
 2 #include<stack>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<cstdlib>
 8 #include<numeric>
 9 #include<vector>
10 #include<ctime>
11 #include<queue>
12 #include<list>
13 #include<map>
14 #define pi acos(-1)
15 #define INF 0x7fffffff
16 #define clr(x)  memset(x,0,sizeof(x));
17 #define clrto(x,siz,y)  for(int xx=0;xx<=siz;xx++)  x[xx]=y;
18 #define clrset(x,siz)  for(int xx=0;xx<=siz;xx++)  x[xx]=xx;
19 #define clrvec(x,siz) for(int xx=0;x<=siz;xx++)  x[xx].clear();
20 #define fop   freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
21 #define myprogram By_135678942570
22 #define clrcpy(x,siz,y)  for(int xx=0;xx<siz;xx++)  x[xx]=y[xx];
23 using namespace std;
24 struct node
25 {
26      int val;
27      int emp;
28      int to;
29 };
30 vector<node>road[111];
31 int e[11]={0};
32 int f[11]={0};
33 int use[11]={0};
34 int vis[111]={0};
35 int n,m,q;
36 long long minn=INF;
37 void find(int pos,int sum)
38 {
39      if(sum>=minn)
40         return;
41      if(pos==n-1)
42      {
43          minn=sum;
44          return;
45      }
46      for(int i=0;i<road[pos].size();i++)
47      {
48         int next=road[pos][i].to;
49         if(!vis[next])
50         {
51              vis[next]=1;
52              int k=road[pos][i].emp;
53              int c=road[pos][i].val;
54              use[k]++;
55              if(use[k]==1)
56                  find(next,sum+c);
57              else if(use[k]==2)
58                  find(next,sum+c+e[k]);
59              else find(next,sum+c+f[k]);
60              vis[next]=0;
61              use[k]--;
62         }
63      }
64 }
65 main()
66 {
67      while(scanf("%d%d%d",&n,&m,&q)!=EOF)
68      {
69          clr(vis);
70          clr(use);
71          for(int i=0;i<=100;i++)
72             road[i].clear();
73          for(int i=0;i<m;i++)
74             scanf("%d",e+i);
75          for(int i=0;i<m;i++)
76             scanf("%d",f+i);
77          for(int i=0;i<q;i++)
78          {
79              int a,b,c,d;
80              scanf("%d%d%d%d",&a,&b,&c,&d);
81              node temp;
82              temp.to=b;
83              temp.val=d;
84              temp.emp=c;
85              road[a].push_back(temp);
86          }
87          minn=INF;
88          find(0,0);
89          if(minn==INF)
90             puts("-1");
91          else printf("%d
",minn);
92      }
93      return 0;
94 }
原文地址:https://www.cnblogs.com/Annetree/p/6645571.html