【题意】
输入三元组(X,Y,C),有向图,定根0,输出MDST。
Input
The first line of input gives the number of cases, N (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, u, v 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.
Sample Input
4
2
1
0 1 10
2
1
1 0 10
4
4
0 1 10
0 2 10
1 3 20
2 3 30
4
4
0 1 10
1 2 20
2 0 30
2 3 100
Sample Output
Case #1: 10
Case #2: Possums!
Case #3: 40
Case #4: 130
【分析】
裸的MDST。
哇,自己打一下真是各种bug orz。。。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010
#define Maxm 40010
#define INF 0xfffffff struct node
{
int x,y,c;
}t[Maxm]; int in[Maxn],vis[Maxn],id[Maxn],pre[Maxn]; int n,m,rt; int MDST()
{
int ans=;
rt=;
while()
{
for(int i=;i<=n;i++) in[i]=INF;
for(int i=;i<=m;i++)
{
int x=t[i].x,y=t[i].y;
if(t[i].c<in[y]&&x!=y)
{
in[y]=t[i].c;
pre[y]=x;
}
}
for(int i=;i<=n;i++) if(i!=rt&&in[i]==INF) return -;
memset(vis,-,sizeof(vis));
memset(id,-,sizeof(id));
int cnt=;
for(int i=;i<=n;i++) if(i!=rt)
{
ans+=in[i];
int now=i;
while(vis[now]!=i&&id[now]==-&&now!=rt)
{
vis[now]=i;
now=pre[now];
}
if(now!=rt&&id[now]==-)
{
cnt++;
for(int j=pre[now];j!=now;j=pre[j])
id[j]=cnt;
id[now]=cnt;
}
}
if(cnt==) break;
for(int i=;i<=n;i++) if(id[i]==-) id[i]=++cnt;
for(int i=;i<=m;i++)
{
int x=t[i].x,y=t[i].y;
t[i].x=id[x];t[i].y=id[y];
if(t[i].x!=t[i].y) t[i].c-=in[y];
}
rt=id[rt];
n=cnt; }
return ans;
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].c);
t[i].x++;t[i].y++;
}
printf("Case #%d: ",++kase);
int x=MDST();
if(x==-) printf("Possums!\n");
else printf("%d\n",x);
}
return ;
}
2016-11-01 14:42:51