#P1078. [NOIP 2012 普及组] 文化之旅(疑似错题)

    ID: 78 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>搜索图论2012NOIp 普及组剪枝最短路

[NOIP 2012 普及组] 文化之旅(疑似错题)

Description

A messenger plans to travel among countries. Each time he enters a country, he learns that country's culture. He refuses to learn any culture more than once; that is, once he has learned a culture, he cannot visit any other country that has the same culture. Different countries may share the same culture. Different cultures view other cultures differently: some cultures reject certain foreign cultures. Specifically, if he has learned a culture, then he cannot enter any country whose local culture rejects that learned culture. He also learns the local culture at both the start and the end.

Given the geography (countries and roads), each country's culture, each culture's view of other cultures, the start and end countries, and road distances, find the minimum total distance from the start to the end.

Input Format

The first line contains five integers N,K,M,S,TN, K, M, S, T separated by single spaces, denoting the number of countries (indexed 11 to NN), the number of cultures (indexed 11 to KK), the number of roads, and the indices of the start and the end, respectively. It is guaranteed that STS \ne T.

The second line contains NN integers. The ii-th integer CiC_i denotes the culture of country ii.

The next KK lines each contain KK integers separated by single spaces. Let the jj-th integer on the ii-th line be aija_{ij}. If aij=1a_{ij} = 1, then culture ii rejects visitors who have learned culture jj (when i=ji = j, it means rejecting visitors of the same culture). If aij=0a_{ij} = 0, it does not reject. Note that ii rejecting jj does not imply jj rejects ii.

The next MM lines each contain three integers u,v,du, v, d separated by single spaces, indicating an undirected road between countries uu and vv with distance dd. It is guaranteed that uvu \ne v, and there may be multiple roads between the same pair of countries.

Output Format

Output a single integer: the minimum total distance for the messenger to travel from SS to TT. If it is impossible, output 1-1.

2 2 1 1 2 
1 2 
0 1 
1 0 
1 2 10 

-1
2 2 1 1 2 
1 2 
0 1 
0 0 
1 2 10 
10

Hint

Explanation for Sample 1: Since reaching country 22 must pass through country 11, and the culture of country 22 rejects the culture of country 11, it is impossible to reach country 22.

Explanation for Sample 2: The route is 121 \to 2.

Constraints:

  • For 100%100\% of the testdata:
    • 2N1002 \le N \le 100.
    • 1K1001 \le K \le 100.
    • 1MN21 \le M \le N^2.
    • 1CiK1 \le C_i \le K.
    • 1u,vN1 \le u, v \le N.
    • 1d10001 \le d \le 1000.
    • 1S,TN1 \le S, T \le N.
    • STS \ne T.

This is NOIP 2012 Junior Problem 4.

Translated by ChatGPT 5