一道数据十分水的题目,出自2012年的普及组。各种玄学算法都能过,最常见的是倒着搜的dfs。
我是在水最短路的时候刷到这个题的,因为喜欢用bfs,所以写了这个题解。
思路来自:洛谷dalao lushangyin的题解
这个题的数据很小,n只有100,直接使用二维数组保存数据
也很像前向星的存边方法
定义:
a[x][i].to表示点x的第i条边所到达的点
a[x][i].dis表示这条边的权值
mapp[i][j]表示文化 i 与文化 j 之间的关系
c[i]表示点 i 所有的文化
vis[i][j]判断点 i 与点 j 之间文化目前状态
cnt[i] 表示点i 已经有几条边了
- #include<bits/stdc++.h> //大家不要学
- using namespace std;
- struct gjc{//仅出自个人习惯
- int to;
- int dis;
- } a[110][110];
- int cnt[110];
- int n,m,k,str,en;
- int c[110],d[110];
- int mapp[110][110];
- int vis[2001][101];
- void read(){//初始化
- cin>>n>>k>>m>>str>>en;
- for(int i=1;i<=n;i++)
- cin>>c[i];
- for(int i=1;i<=k;i++)
- for(int j=1;j<=k;j++)
- cin>>mapp[i][j];
- for(int i=1;i<=m;i++)
- {
- int x,y,z;
- cin>>x>>y>>z;
- cnt[x]++;cnt[y]++;//边数加一
- a[x][cnt[x]].to=y;a[x][cnt[x]].dis=z;//希望能理解
- a[y][cnt[y]].to=x;a[y][cnt[y]].dis=z;
- }
- }
- void out()//输出
- {
- if(d[en]==0) cout<<"-1";
- else cout<<d[en];
- }
-
- void bfs(){//不用解释
- priority_queue<int,vector<int>,greater<int> > que;//定义一个小根堆优化的优先队列 后来发现普通的队列好像也可以
- int node;
- node=str;
- que.push(node);//将起点推入队列
- for(int i=1;i<=n;i++)
- if(mapp[c[i]][c[str]]==1) vis[str][i]=1;
- else if(c[i]==c[str])vis[str][i]=1;//改变文化状态 根据题目大意很好理解
- while(!que.empty())
- {
- int v=que.top();que.pop();//取出队首元素(是一个点)
- for(int i=1;i<=cnt[v];i++)//遍历这个点的所有边
- {
- node=a[v][i].to;//node存储下一个点的编号
- if(vis[v][node]!=0) continue;//如果当前点和目标点文化有冲突就跳过
- if(d[node]>a[v][i].dis+d[v]||d[node]==0)//如果这么走更短或是第一次走
- {
-
- for(int j=1;j<=n;j++)//和上面对起点文化的改变很像
- {
- if(mapp[c[j]][c[node]==1]) vis[node][j]=1;
- else
- if(vis[v][j]==1) vis[node][j]=1;//理解一下
- else
- if(c[j]==c[node]) vis[node][j]=1;
- }
- d[node]=d[v]+a[v][i].dis;
- que.push(node);//将缩短路径的点推入队列中
- }
- }
- }
- }
-
-
- int main(){
- read();
- bfs();
- out();
- return 0;
- }