NOIP 2012 cultural tour

I saw that the data of this problem mentioned in Luogu was especially water, so I wrote a very water approach. ...
Topic background
Title Description
Input format
Output format
Input and output samples
Description / tips

I saw that the data of this problem mentioned in Luogu was especially water, so I wrote a very water approach.

Title: P1078 [NOIP2012 popularization group] cultural tour - New Ecology of computer science education in Luogu (luogu.com.cn)

Topic background

This problem is wrong. It was later proved that there is no reliable practice of polynomial complexity. The test data is very watery, and various metaphysical practices can pass (such as reverse scanning), which does not mean that the algorithm is correct. Therefore, the title and data are for reference only.

Title Description

There is a messenger who wants to travel around countries. Every time he goes to a country, he can learn a culture, but he is unwilling to learn any culture more than once (that is, if he learns a culture, he can't reach other countries with this culture). Different countries may have the same culture. Countries with different cultures have different views on other cultures. Some cultures will exclude foreign cultures (that is, if he learns a certain culture, he cannot reach other countries that exclude this culture).

Given the geographical relationship between countries, the culture of each country, the views of each culture on other cultures, as well as the starting point and end point of the envoy's tour (local culture will also be learned at the starting point and end point), the road distance between countries, try to find out the minimum distance to go from the starting point to the end.

Input format

The first line is five integers   N. K, m, s, TN, K, m, s, t are separated by a space between every two integers, representing the number of countries in turn (country numbers are 11 to   NN), number of cultural species (Cultural number is 11 to KK), number of roads, and number of starting and ending points (guaranteed)   SS   Not equal to TT);

The second line is NN integers, and every two integers are separated by a space, where the second line is   ii number C_iCi, indicating that the culture of country ii is C_iCi​.

Next   KK line, KK integers in each line, separated by a space between each two integers, record the second line   The j-th number of rows is a_aij​,a_= 1aij​=1   Express culture   ii exclusion of foreign cultures JJ (ii)   When equal to JJ, it means to exclude outsiders of the same culture), a_= 0aij​=0   Indicates no exclusion (note ii)   exclude   jj   There is no guarantee that JJ will also exclude ii).

Next   MM   Rows, three integers per row   u. V, Du, V, D, every two integers are separated by a space, indicating the country   uu and country   vv has a two-way road with a distance of dd (ensure that uu is not equal to   vv, there may be multiple roads between two countries).

Output format

An integer representing the minimum distance that the messenger needs to travel from the starting country to the ending country (if there is no solution, output - 1 − 1).

Input and output samples

Enter #1

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

Output #1

-1

Enter #2

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

Output #2

10

Description / tips

Input / output example description 11

Because to the country   twenty-two   It must pass through country 11, while the civilization of country 22 excludes countries   eleven   Civilization, so it is impossible to reach the country   22.

Input / output example description 22

Route 11  -> twenty-two

[data range]

For 100% data, there are 2 ≤ N ≤ 1002 ≤ N ≤ 100

1≤K≤1001≤K≤100

1≤M≤N^21≤M≤N2

1≤k_i≤K1≤ki​≤K

1≤u, v≤N1≤u,v≤N

1≤d≤1000,S≠T,1≤S,T≤N1≤d≤1000,S=T,1≤S,T≤N

Question 4 of NOIP 2012 popularization group

Solution: since the data is very watery, let's try explosive search:

Idea: for each node to go, judge whether there is a node in conflict with him or a node that has been to.

Because the size of each edge is greater than 1, it is obvious that the answer is definitely not the smallest if you walk around the ring.

Do not use a stack to store every node. Remember to backtrack.

code:

#include <bits/stdc++.h> using namespace std; struct node { int f,t,val,nex; }rt[10010]; int head[10010],cnt,vis[1010][1010],qaq[10010]; int be,ed,a,b,c,lo[10010]; int bec; void add(int x,int y,int z) { cnt ++; rt[cnt].f = x; rt[cnt].t = y; rt[cnt].val = z; rt[cnt].nex = head[x]; head[x] = cnt; } int sta[1010]; int ans = 0x3f3f3f3f; void dfs(int x,int num,int co) { if(clock() - bec > 999900) { if(ans == 0x3f3f3f3f) cout<<-1; else cout<<ans; exit(0); } if(x == ed){ ans = min(ans,co); } else { for(int i = head[x];i;i = rt[i].nex) { bool faq = 1; for(int j = 1;j <= num;j ++) { if(qaq[rt[i].t]) faq = 0; if(vis[lo[rt[i].t]][lo[sta[j]]] || lo[rt[i].t] == lo[sta[j]]) faq = 0; } if(faq) { qaq[rt[i].t] = 1; sta[num + 1] = rt[i].t; dfs(rt[i].t,num + 1,co + rt[i].val); qaq[rt[i].t] = 0; } } } } int main() { bec = clock(); cin>>a>>c>>b>>be>>ed; for(int i = 1;i <= a;i ++) { cin>>lo[i]; } int x,y; for(int i = 1;i <= c;i ++) { for(int j = 1;j <= c;j ++) { cin>>x; vis[i][j] = x; } } int z; for(int i = 1;i <= b;i ++) { cin>>x>>y>>z; add(x,y,z); add(y,x,z); } qaq[be] = 1; sta[1] = be; dfs(be,1,0); if(ans == 0x3f3f3f3f) cout<<-1; else cout<<ans; return 0; } /* 10 5 18 1 7 4 2 5 3 2 2 1 2 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 656 2 4 989 2 1 947 3 2 731 4 10 830 4 8 688 5 6 573 5 3 492 6 10 700 6 3 854 8 7 839 8 7 461 10 9 885 10 9 960 4 7 4 3 4 8 2 3 4 1 2 5 */

Because the complexity of explosive search is certainly unacceptable, you can output it when you want to timeout. Anyway, the data is very water. [doge][doge][doge].

19 November 2021, 13:47 | Views: 3405

Add new comment

For adding a comment, please log in
or create account

0 comments