给点各边的连接关系,求最小环,如果给定的是点的关系,可以用floyd简单的求出,但是现在给定的是边的关系,如果转换成点的关系,将会很麻烦,然而最
小环也可以用dijkstra来求,枚举每条边,求出到自己的最短路即为最小环的长度
- #include <iostream>
- #include <algorithm>
- using namespace std;
- /*
- PROG: fence6
- LANG: C++
- ID: heben991
- */
- const int N = 220, INF = 100000000;
- int v, e, w[N], num[N], pos[N],
- l[N][2][N], ans;
- int dist[N], pre[N];
- bool mark[N];
- int dijkstra(int s)
- {
- int i, j, k, t, u, v;
- for(i = 1; i <= e; ++i)
- {
- dist[i] = INF;
- mark[i] = 0;
- }
- for(i = 1; i <= l[s][0][0]; ++i)
- {
- t = l[s][0][i];
- dist[t] = w[s];
- pre[t] = s;
- }
- while(1)
- {
- u = 0; t = INF;
- for(i = 1; i <= e; ++i)
- if(!mark[i] && dist[i] < t)
- {
- t = dist[i];
- u = i;
- }
- if(0 == u)break;
- if(s == u) return dist[s];
- mark[u] = 1;
- for(i = 1; i <= l[u][0][0]; ++i)
- if(pre[u] == l[u][0][i]) break;
- if(i <= l[u][0][0]) k = 1;
- else k = 0;
- for(i = 1; i <= l[u][k][0]; ++i)
- {
- v = l[u][k][i];
- if( dist[u] + w[u] < dist[v] )
- {
- dist[v] = dist[u] + w[u];
- pre[v] = u;
- }
- }
- }
- return INF;
- }
- int main()
- {
- int i, j, k, t;
- freopen("fence6.in", "r", stdin);
- freopen("fence6.out","w",stdout);
- scanf("%d", &e);
- for(i = 1; i <= e; ++i)
- {
- scanf("%d%d%d%d", &num[i], &w[i], l[i][0], l[i][1]);
- for(j = 0; j < 2; ++j)
- {
- for(k = 1; k <= l[i][j][0]; ++k)
- {
- scanf("%d", &l[i][j][k]);
- }
- }
- }
- for(i = 1; i <= e; ++i)
- {
- pos[num[i]] = i;
- }
- for(i = 1; i <= e; ++i)
- for(j = 0; j < 2; ++j)
- for(k = 1; k <= l[i][j][0]; ++k)
- l[i][j][k] = pos[ l[i][j][k] ];
- ans = INF;
- for(i = 1; i <= e; ++i)
- {
- ans <?= dijkstra(i);
- }
- printf("%d/n",ans);
- return 0;
- }