定義
在一張無向帶權圖中,找出一棵生成樹,使得某個特定結點的度數不超過
求解算法
首先我們對不與特殊結點相連的邊求最小生成樹,得到幾個連通分量。然後找出每個連通分量到特殊結點的最短的邊,把這條邊添加到生成樹中,這樣我們就得到了一個度限制爲連通分量數的最小生成樹。如果有連通分量到特殊結點沒有邊,則說明無法構成生成樹。如果連通分量數大於
對於當前求得的K度生成樹,我們可以求出
總時間複雜度
代碼(POJ1639)
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define mem(a,b) memset((a),(b),sizeof(a))
const int MAXLEN=10+3;
const int MAXV=20+3;
const int MAXE=MAXV*MAXV;
struct Edge
{
int from, to, cost;
bool operator < (const Edge &other)const
{
return cost<other.cost;
}
}edge[MAXE];//保存圖中的所有邊
int V, E, K;//結點數,邊數,度數限制
int par[MAXV], high[MAXV];//並查集數組
int G[MAXV][MAXV];//圖的鄰接矩陣表示(沒有邊,花費爲INF)
bool in_tree[MAXV][MAXV];//在生成樹中標記
int the_min[MAXV];//連通塊到特殊點的最近的邊
Edge dp[MAXV];
int findfather(int x)
{
return par[x]=par[x]==x?x:findfather(par[x]);
}
bool unite(int a, int b)//並查集合並
{
int fa=findfather(a), fb=findfather(b);
if(fa==fb)
return false;
if(high[fa]>high[fb])
par[fb]=fa;
else
{
par[fa]=fb;
if(high[fa]==high[fb])
++high[fb];
}
return true;
}
void dfs(int u, int fa)
{
for(int v=1;v<=V;++v)
if(v!=fa && in_tree[u][v])
{
if(fa==-1)
dp[v].cost=-INF;
else
{
if(G[u][v]>dp[u].cost)
{
dp[v].from=u;
dp[v].to=v;
dp[v].cost=G[u][v];
}
else dp[v]=dp[u];
}
dfs(v, u);
}
}
bool degree_limit_tree()//返回是否能構成滿足要求的樹,形成的樹保存在in_tree數組中
{
//初始化
for(int i=1;i<=V;++i)
{
par[i]=i;
high[i]=0;
for(int j=1;j<=V;++j)
in_tree[i][j]=false;
}
//Kruskal計算求不與特殊點連接的邊的最小生成樹
sort(edge+1, edge+1+E);
for(int i=1;i<=E;++i)
{
if(edge[i].from>edge[i].to)
swap(edge[i].from, edge[i].to);
if(edge[i].from!=1 && unite(edge[i].from, edge[i].to))
in_tree[edge[i].from][edge[i].to]=in_tree[edge[i].to][edge[i].from]=true;
}
//把各個連通塊連接到特殊點
for(int i=1;i<=V;++i)
the_min[i]=-1;
for(int i=1;i<=E;++i)
if(edge[i].from==1)
{
int fa=findfather(edge[i].to);
if(the_min[fa]==-1 || edge[i].cost<edge[the_min[fa]].cost)
the_min[fa]=i;
}
int deg=0;//根節點度數
for(int i=2;i<=V;++i)
if(~the_min[i])
{
int id=the_min[i];
in_tree[edge[id].from][edge[id].to]=G[edge[id].to][edge[id].from]=true;
++deg;
}
else if(the_min[findfather(i)]==-1)//有連通塊無法連接到根節點
return false;
if(deg>K)//無法在限制內構成生成樹
return false;
dp[1].cost=-INF;
//不斷增加特殊結點的度數
for(++deg;deg<=K;++deg)
{
int add=INF, index=-1;
dfs(1, -1);
for(int v=2;v<=V;++v)
if(G[1][v]!=INF && !in_tree[1][v] && add>G[1][v]-dp[v].cost)
{
add=G[1][v]-dp[v].cost;
index=v;
}
if(add>=0)//不能使答案更小,跳出
break;
in_tree[1][index]=in_tree[index][1]=true;
in_tree[dp[index].from][dp[index].to]=in_tree[dp[index].to][dp[index].from]=false;
}
return true;
}
map<string, int> to_id;
inline int get_id(char s[])
{
int res=to_id[s];
if(!res)
to_id[s]=res=++V;
return res;
}
void init()
{
V=1;
to_id.clear();
to_id["Park"]=1;
mem(G, 0x3f);
}
int main()
{
while(~scanf("%d", &E))
{
init();
for(int i=1;i<=E;++i)
{
char s[MAXLEN];
scanf("%s", s);
edge[i].from=get_id(s);
scanf("%s", s);
edge[i].to=get_id(s);
scanf("%d", &edge[i].cost);
if(G[edge[i].from][edge[i].to]>edge[i].cost)
G[edge[i].from][edge[i].to]=G[edge[i].to][edge[i].from]=edge[i].cost;
}
scanf("%d", &K);
degree_limit_tree();
int ans=0;
for(int i=1;i<=V;++i)
for(int j=i+1;j<=V;++j)
if(in_tree[i][j])
ans+=G[i][j];
printf("Total miles driven: %d\n", ans);
}
return 0;
}