codevs1316 Culture Tour

Source: Internet
Author: User

Title Description Description

There is a messenger who travels to all countries, and he learns a culture in every country, but he does not want to learn any culture more than once (that is, if he learns a certain culture, he cannot reach other countries with this culture). Different countries may have the same culture. Different cultures have different perceptions of other cultures, and some cultures exclude foreign cultures (that is, if he learns a certain culture, he cannot reach other countries that exclude it).

Given the geographical relations among nations, the cultures of each country, the views of each culture on other cultures, and the beginning and end of the messenger's Journey (learning the local culture at the beginning and end), the road distance between countries, and the minimum number of roads to be traveled from the beginning to the end.

Enter a description Input Description

The first behavior is five integer n,k,m,s,t, each two integers separated by a space, which in turn represents the number of countries (country number 1 to n), the number of cultural species (Culture number 1 to K), the number of roads, and the starting and ending numbers (guaranteed S not equal to T);

The second act n integers, separated by a space between each of the two integers, the number I CI, which indicates that the culture of country I is CI.

The next K-line, each line of K-integers, each two integers separated by a space, the first row of the number of J is aij,aij= 1 for the culture I exclude foreign culture J (I equals J when the foreigner to reject the same culture), aij= 0 means that the non-exclusion (note I exclusion J does not guarantee that J must also repel I).

The next M-line, each line of three integers u,v,d, separated by a space between each of the two integers, indicates that the country U and the State v have a two-way road with a distance of D (guaranteed u not equal to V, there may be multiple roads between two countries).

Output description Output Description

The output is only one line, an integer, indicating the number of distances the messenger must travel from the country of origin to the destination country (if no solution is output-1).

Sample input Sample Input

Input Example 1

2 2 1) 1 2

1 2

0 1

1 0

1 2 10

Input Example 2

2 2 1) 1 2

1 2

0 1

0 0

1 2 10

Sample output Sample Output

Output Example 1

-1

Output Example 2

10

Data range and Tips Data Size & Hint

"Input and Output Example 1 description"

Since to the country 2 must go through the state 1, and the country 2 of the civilization has excluded the state 1 civilization, it is impossible to reach the country 2.

"Input and Output Example 2 description"

The route is 1-2.

"Data Range"

For 20% of the data, there are 2≤n≤8,k≤5;

For 30% of the data, there are 2≤n≤10,k≤5;

For 50% of the data, there are 2≤n≤20,k≤8;

For 70% of the data, there are 2≤n≤100,k≤10;

For 100% of the data, there are 2≤n≤100,1≤k≤100,1≤m≤n2,1≤ki≤k,1≤u,v≤n,1≤d≤1000,s≠t,1≤s, t≤n.

/*weird, short, weird test data .*/#include<iostream>#include<cstring>using namespacestd;BOOLec[101][101];intmp[101][101];intct[101],study[101];intdis[ -];intN,k,m,bg,ed,flag;BOOLPdintx) {     for(intI=1; i<=k;i++)    {        if(Study[i]&&ec[ct[x]][i])return 0; }    return 1;}intSearchintx) {Flag=0; if(!Study[ct[x]]) {Flag=1; STUDY[CT[X]]=1;}  for(intI=1; i<=n;i++)    {        if(MP[X][I]+DIS[X]&LT;DIS[I]&AMP;&AMP;PD (i)) {dis[i]=mp[x][i]+Dis[x];search (i);} }    if(flag==1) study[ct[x]]=0;}intMain () {CIN>>n>>k>>m>>bg>>Ed;  for(intI=1; i<=n;i++) Cin>>Ct[i];  for(intI=1; i<=k;i++)        for(intj=1; j<=k;j++) Cin>>Ec[i][j]; Memset (MP,127/3,sizeof(MP));  for(intI=1; i<=n;i++) dis[i]=999999;  for(intI=1; i<=m;i++)    {        intx,y,v; CIN>>x>>y>>v; Mp[x][y]=mp[y][x]=W; } DIS[BG]=0;    Search (BG); if(dis[ed]!=999999) cout<<dis[ed]; Elsecout<<-1; return 0;}

codevs1316 Culture Tour

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.