A new Graph Game
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1010 Accepted Submission(s): 454
Problem Description
An undirected graph is a graph in which the nodes are connected by undirected arcs. An undirected arc is an edge that has no arrow. Both ends of an undirected arc are equivalent--there is no head or tail. Therefore, we represent an edge in an undirected graph as a set rather than an ordered pair.
Now given an undirected graph, you could delete any number of edges as you wish. Then you will get one or more connected sub graph from the original one (Any of them should have more than one vertex).
You goal is to make all the connected sub graphs exist the Hamiltonian circuit after the delete operation. What’s more, you want to know the minimum sum of all the weight of the edges on the “Hamiltonian circuit” of all the connected sub graphs (Only one “Hamiltonian circuit” will be calculated in one connected sub graph! That is to say if there exist more than one “Hamiltonian circuit” in one connected sub graph, you could only choose the one in which the sum of weight of these edges is minimum).
For example, we may get two possible sums:
(1) 7 + 10 + 5 = 22
(2) 7 + 10 + 2 = 19
(There are two “Hamiltonian circuit” in this graph!)
Input
In the first line there is an integer T, indicates the number of test cases. (T <= 20)
In each case, the first line contains two integers n and m, indicates the number of vertices and the number of edges. (1 <= n <=1000, 0 <= m <= 10000)
Then m lines, each line contains three integers a,b,c ,indicates that there is one edge between a and b, and the weight of it is c . (1 <= a,b <= n, a is not equal to b in any way, 1 <= c <= 10000)
Output
Output “Case %d: “first where d is the case number counted from one. Then output “NO” if there is no way to get some connected sub graphs that any of them exists the Hamiltonian circuit after the delete operation. Otherwise, output the minimum sum of weight you may get if you delete the edges in the optimal strategy.
Sample Input
3
3 4
1 2 5
2 1 2
2 3 10
3 1 7
3 2
1 2 3
1 2 4
2 2
1 2 3
1 2 4
Sample Output
Case 1: 19
Case 2: NO
Case 3: 6
Hint
In Case 1: You could delete edge between 1 and 2 whose weight is 5. In Case 2: It’s impossible to get some connected sub graphs that any of them exists the Hamiltonian circuit after the delete operation.
Author
AekdyCoin
Source
2010 ACM-ICPC Multi-University Training Contest(1)——Host by FZU
Recommend
zhouzeyong
刚开始不明题意~~~其实与
3488
Tour 1853Cyclic Tour [费用流]圈基本一样
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 99999999
using namespace std;
struct node
{
int u,v,f,c;
};
node e[60000];
int first[3000],next[60000],cc;
int p[3000],preedge[3000],d[3000],ans_flow,ans_cost;
inline void add_edge(int u,int v,int f,int c)
{
e[cc].u=u;
e[cc].v=v;
e[cc].f=f;
e[cc].c=c;
next[cc]=first[u];
first[u]=cc;
cc++;
e[cc].v=u;
e[cc].u=v;
e[cc].f=0;
e[cc].c=-c;
next[cc]=first[v];
first[v]=cc;
cc++;
}
bool spfa(int s,int t)
{
int inq[3000];
memset(inq,0,sizeof(inq));
memset(p,-1,sizeof(p));
memset(preedge,-1,sizeof(preedge));
int i;
for(i=0;i<=t;i++)
d[i]=inf;
d[s]=0;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=0;
for(i=first[u];i!=-1;i=next[i])
{
if(e[i].f)
{
int v=e[i].v;
if(d[v]>d[u]+e[i].c)
{
d[v]=d[u]+e[i].c;
p[v]=u;
preedge[v]=i;
if(!inq[v])
{
inq[v]=1;
q.push(v);
}
}
}
}
}
if(d[t]>=inf)
return false;
else
return true;
}
void min_cost_flow(int s,int t)
{
ans_cost=0;ans_flow=0;
while(spfa(s,t))
{
int u=t;
int mm=inf;
while(p[u]!=-1)
{
mm=min(mm,e[preedge[u]].f);
u=p[u];
}
u=t;
while(p[u]!=-1)
{
e[preedge[u]].f-=mm;
e[preedge[u]^1].f+=mm;
u=p[u];
}
ans_cost+=mm*d[t];
ans_flow+=mm;
}
}
int main()
{
int tt;
scanf("%d",&tt);
int cas;
for(cas=1;cas<=tt;cas++)
{
int n,m;
scanf("%d%d",&n,&m);
int i;
int s=0;
int t=2*n+1;
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
cc=0;
for(i=1;i<=n;i++)
{
add_edge(s,i,1,0);
add_edge(n+i,t,1,0);
}
for(i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v+n,1,w);
add_edge(v,u+n,1,w);
}
min_cost_flow(s,t);
if(ans_flow<n)
printf("Case %d: NO\n",cas);
else
printf("Case %d: %d\n",cas,ans_cost);
}
return 0;
}