Uva 11183 - Teen Girl Squad (最小树形图)

Problem I
Teen Girl Squad 
Input: Standard Input Output: Standard Output

You are part of a group of n teenage girls armed with cellphones. You have some news you want to tell everyone in the group. The problem is that no two of you are in the same room, and you must communicate using only cellphones. What's worse is that due to excessive usage, your parents have refused to pay your cellphone bills, so you must distribute the news by calling each other in the cheapest possible way. You will call several of your friends, they will call some of their friends, and so on until everyone in the group hears the news.

Each of you is using a different phone service provider, and you know the price of girl A calling girl B for all possible A and B. Not all of your friends like each other, and some of them will never call people they don't like. Your job is to find the cheapest possible sequence of calls so that the news spreads from you to all n-1 other members of the group.

Input

The first line of input gives the number of cases, (N<150). N test cases follow. Each one starts with two lines containing n (0<=n<=1000)and m (0 <= m <= 40,000). Girls are numbered from 0 to n-1, and you are girl 0. The next m lines will each contain 3 integers, uv and w, meaning that a call from girl u to girl v costs w cents (0 <= w <= 1000). No other calls are possible because of grudges, rivalries and because they are, like, lame. The input file size is around 1200 KB.

Output

For each test case, output one line containing "Case #x:" followed by the cost of the cheapest method of distributing the news. If there is no solution, print "Possums!" instead.


Problem setter: Igor Naverniouk
 
解题思路:最小树形图 模板题
 
 #include<cstdio>
#include<cstring>
#define SIZE 1002
#define MAXN 40004 using namespace std; const int INF = <<;
int nv, ne, N;
int vis[SIZE], pre[SIZE], num[SIZE];
int inLength[SIZE]; struct Edge{
int u, v, w;
}edge[MAXN]; bool Traverse(int& res)
{
int root = ;
while(true)
{
for(int i=; i<nv; ++i) inLength[i] = INF;
for(int i=; i<ne; ++i)
{
int& u = edge[i].u;
int& v = edge[i].v;
if(edge[i].w < inLength[v] && u != v)
{
inLength[v] = edge[i].w;
pre[v] = u;
}
}
for(int i=; i<nv; ++i)
if(i != root && inLength[i] == INF) return false;
int newnum = ;
inLength[root] = ;
memset(vis, -, sizeof(vis));
memset(num, -, sizeof(num));
for(int i=; i<nv; ++i)
{
res += inLength[i];
int v = i;
while(vis[v] != i && num[v] == - && v != root)
{
vis[v] = i;
v = pre[v];
}
if(vis[v] == i)
{
for(int u=pre[v]; u != v; u = pre[u])
num[u] = newnum;
num[v] = newnum++;
}
}
if(newnum == ) return true;
for(int i=; i<nv; ++i)
if(num[i] == -) num[i] = newnum++;
for(int i=; i<ne; ++i)
{
int u = edge[i].u;
int v = edge[i].v;
edge[i].u = num[u];
edge[i].v = num[v];
if(edge[i].u != edge[i].v)
edge[i].w -= inLength[v];
}
nv = newnum;
root = num[root];
} } int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
scanf("%d", &N);
for(int t = ; t <= N; ++t)
{
int u, v, w;
scanf("%d%d", &nv, &ne);
for(int i=; i<ne; ++i)
{
scanf("%d%d%d", &u, &v, &w);
edge[i].u = u;
edge[i].v = v;
edge[i].w = u == v ? INF+ : w;
}
printf("Case #%d: ", t);
int res = ;
if(Traverse(res)) printf("%d\n", res);
else printf("Possums!\n");
}
return ;
}
 

上一篇:kali linux之选择和修改exp与windows后渗透


下一篇:小白日记17:kali渗透测试之缓冲区溢出实例-windows,POP3,SLmail