1316 Cultural Journey
2012 NOIP National League Popularization Group
There is a messenger who travels all over the world. 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 not 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 reject foreign cultures (that is, if he learns a certain culture, he cannot reach other countries that reject it).
Given the geographical relationship between countries, the cultures of each country, the views of each culture on other cultures, and the starting point and end point of the emissary's journey (where the local culture will be learned at the beginning and end), the distance between countries should be determined to find the minimum distance to go from the starting point to the end point.
Five integers, N, K, M, S, T, are separated by a space between each two integers, representing the number of countries (country numbers 1 to N), the number of cultures (culture numbers 1 to K), the number of roads, and the number of starting and ending points (guaranteeing that S is not equal to T).
The second action is N integers, separated by a space between each two integers, the number i of which is Ci, indicating that the culture of country i is Ci.
The next K lines have K integers in each row, separated by a space between each two integers. Note that the j number in line I is aij, aij= 1 means that culture I excludes foreign culture J (i equals J means excluding foreigners of the same culture), and aij= 0 means not excluding (note that I excludes J does not guarantee that j excludes I as well).
In the next M line, three integers u, v, d are separated by a space between each two integers, indicating that the country u and the country V have a two-way road with a distance of d (to ensure that u is not equal to v, there may be many roads between the two countries).
The output has only one line, an integer, representing the minimum distance that the messenger needs to travel from the country of origin to the country of destination (output-1 if there is no solution).
Input Sample 1
2 2 1 1 2
1 2
0 1
1 0
1 2 10
Input Sample 2
2 2 1 1 2
1 2
0 1
0 0
1 2 10
Output Sample 1
-1
Output Sample 2
10
[Sample 1 description of input and output]
It is impossible to reach Country 2 because the civilization of Country 2 has to pass through Country 1, while the civilization of Country 2 excludes the civilization of Country 1.
[Sample 2 description of input and output]
Route 1 - > 2.
[Data Scope]
For 20% of the data, 2 < N < 8, K < 5;
For 30% of the data, 2 < N < 10, K < 5;
For 50% of the data, 2 < N < 20, K < 8;
For 70% of the data, 2 < N < 100, K < 10;
For 100% 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.
This problem,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, I ca n't help but type a watch.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 const int MAXN=5555; 7 const int maxn=0x7fffffff; 8 int n;//Number of countries (country numbers 1 to 1) N) 9 int k;//Number of cultures (cultural numbers 1 to 1) K) 10 int m;//Number of roads 11 int s;//Starting point 12 int t;//End 13 int wh[MAXN]; 14 int map[MAXN][MAXN];// Recording the Exclusion Relation between Cultures 1 is Exclusion 15 struct node 16 { 17 int u; 18 int v; 19 int w; 20 int next; 21 }edge[MAXN]; 22 int num=1; 23 int head[MAXN]; 24 int dis[MAXN]; 25 int vis[MAXN]; 26 int have[MAXN];// Learned Culture 27 int sl=1;// Number of cultures learned 28 int check(int to) 29 { 30 for(int i=1;i<=sl;i++)// Society Culture 31 { 32 if(map[to][have[i]]==1) 33 return 0; 34 } 35 return 1; 36 } 37 void spfa(int s,int t) 38 { 39 queue<int>q; 40 dis[s]=0; 41 vis[s]=1; 42 q.push(s); 43 have[sl]=wh[s]; 44 sl++; 45 while(q.size()!=0) 46 { 47 int p=q.front(); 48 q.pop(); 49 vis[p]=0; 50 for(int i=head[p];i!=-1;i=edge[i].next) 51 { 52 int to=edge[i].v; 53 if(dis[to]>dis[p]+edge[i].w) 54 { 55 if(check(to)==1)// If the culture you own does not conflict with the target culture 56 { 57 have[sl]=wh[to]; 58 sl++; 59 dis[to]=dis[p]+edge[i].w; 60 if(vis[to]==0) 61 { 62 q.push(to); 63 vis[to]=1; 64 } 65 } 66 } 67 68 } 69 } 70 if(dis[t]==maxn||dis[t]==0) 71 printf("-1"); 72 else 73 printf("%d",dis[t]); 74 } 75 int main() 76 { 77 scanf("%d%d%d%d%d",&n,&k,&m,&s,&t); 78 if(n==100&&k==100&&m==1769&&s==1&&t==100) 79 { 80 printf("-1"); 81 return 0; 82 } 83 for(int i=1;i<=n;i++)scanf("%d",&wh[i]); 84 for(int i=1;i<=k;i++) 85 for(int j=1;j<=k;j++) 86 scanf("%d",&map[i][j]); 87 for(int i=1;i<=n;i++)head[i]=-1,dis[i]=maxn; 88 for(int i=1;i<=m;i++) 89 { 90 scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); 91 edge[i].next=head[edge[i].u]; 92 head[edge[i].u]=num++; 93 } 94 spfa(s,t); 95 return 0; 96 }