Luogu P1078 cultural tour, luogup1078
Description
There is a messenger who wants to travel to different countries. He can learn a culture in every country, but he does not want to learn any culture more than once (that is, if he learns a culture, then he cannot reach other countries with such culture ). Different countries may have the same culture. Countries of different cultures have different views on other cultures, and some cultures will reject foreign cultures (that is, if he learns a culture, he cannot reach other countries that reject it ).
Given the geographical relationships between countries, the cultures of each country, and the views of each culture on other cultures, and the starting and ending points of the messenger's journey (the start and end points will also learn the local culture), the Road distance between countries, try to find the minimum route from the start point to the end point.
Input/Output Format
Input Format:
In the first action, five integers, N, K, M, S, and T, are separated by a space, which in turn represents the number of countries (country numbers are 1 to N ), number of Cultures (1 to K), number of roads, and number of the start and end points (ensure that S is not equal to T );
The second act is N integers separated by a space. The number I Ci indicates that the national I culture is Ci.
In the next K rows, each row has K integers, and each two integers are separated by a space. The number of j in row I is aij, aij = 1 indicates culture. I exclude foreign cultures. j. When I is equal to j, it indicates that foreign cultures are excluded ), aij = 0 indicates no rejection (note that I exclusive j does not guarantee that j will reject I ).
In the next M line, each line contains three integers u, v, d, which are separated by a space, indicates that the country u and the country v have a two-way road with d Distance (ensure that u is not equal to v, and there may be multiple roads between the two countries ).
Output Format:
The output contains only one row and an integer, indicating the minimum distance that the messenger needs to walk from the start country to the end country (if there is no solution, the output is-1 ).
Input and Output sample input sample #1:
2 2 1 1 2 1 2 0 1 1 0 1 2 10
Output sample #1:
-1
Input example #2:
2 2 1 1 2 1 2 0 1 0 0 1 2 10
Output sample #2:
10
Description
Input and Output Example 1
Because country 2 must pass through country 1, while country 2's civilization rejects Country 1's civilization, it is impossible to reach country 2.
Input and Output Example 2
Route 1-> 2
[Data Scope]
For 100% of 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
Topic 4 of NOIP 2012 popularization Group
I haven't written a blog for a long time. I have handed in this question for 17 times before AQAQ. This question is very simple. I started to small the array, and I keep making mistakes. After I changed it, download data for a long time. If the start culture is the same as the end culture,-1 should be output. Then, the blogger will not find it until it has been changed for a long time... Read the questions carefully !!!
Ideas:
The spfa + slf optimization template is used to run the shortest point, which is to carefully construct and read the questions, and then implement QWQ as usual! The correct solution for this question is DFS. If you are interested, try QWQ!
Question Portal
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=500; 4 5 int readd() 6 { 7 int aans=0; 8 char ch=getchar(); 9 while(ch<'0'||ch>'9') 10 ch=getchar(); 11 while(ch>='0'&&ch<='9') 12 { 13 aans*=10; 14 aans+=ch-'0'; 15 ch=getchar(); 16 } 17 return aans; 18 } 19 20 int n,k,m,s,t; 21 int c[maxn]; 22 bool vis[maxn]; 23 int flag[maxn][maxn]; 24 int now[maxn]; 25 int dis[maxn]; 26 int js=1; 27 struct node{ 28 int net; 29 int to; 30 int w; 31 }a[10005]; 32 int cnt,head[maxn]; 33 34 inline void add(int i,int j,int w) 35 { 36 a[++cnt].to=j; 37 a[cnt].net=head[i]; 38 a[cnt].w=w; 39 head[i]=cnt; 40 } 41 inline void spfa(int s) 42 { 43 deque<int>q; 44 for(int i=1; i<=maxn; i++) 45 dis[i]=2147483647; 46 memset(vis,false,sizeof(vis)); 47 q.push_back(s); 48 dis[s]=0; 49 vis[s]=true; 50 while(!q.empty()) 51 { 52 int u=q.front(); 53 q.pop_front(); 54 vis[u]=false; 55 for(int i=head[u]; i; i=a[i].net) 56 { 57 int v=a[i].to; 58 if(dis[v]>dis[u]+a[i].w) 59 { 60 dis[v]=dis[u]+a[i].w; 61 if(!vis[v]) 62 { 63 vis[v]=true; 64 if(q.empty()||dis[v]>dis[q.front()]) 65 { 66 q.push_back(v); 67 } 68 else 69 { 70 q.push_front(v); 71 } 72 } 73 } 74 } 75 } 76 } 77 78 int main() 79 { 80 n=readd();k=readd();m=readd();s=readd();t=readd(); 81 for(int i=1;i<=n;i++) 82 { 83 c[i]=readd(); 84 } 85 for(int i=1;i<=k;i++) 86 { 87 for(int j=1;j<=k;j++) 88 { 89 flag[i][j]=readd(); 90 } 91 } 92 for(int i=1;i<=m;i++) 93 { 94 int u,v,d; 95 u=readd();v=readd();d=readd(); 96 if(!flag[c[u]][c[v]]) 97 { 98 add(u,v,d); 99 }100 if(!flag[c[v]][c[u]])101 {102 add(v,u,d);103 }104 } 105 spfa(s);106 if(c[s]==c[t])107 {108 cout<<-1<<endl;109 return 0;110 }111 if(dis[t]==2147483647)112 {113 cout<<-1<<endl;114 }115 else116 printf("%d\n",dis[t]);117 return 0;118 }