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
Proceed to the end of file.
Output
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.****.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 ;
}