http://acm.hdu.edu.cn/showproblem.php?pid=3435

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
#define maxn 54444
using namespace std; queue<int>q;
struct node
{
int u,v,next,val,f;
} p[maxn];
int head[maxn];
bool vis[maxn];
int dis[maxn];
int pre[maxn];
int n,m,s,t,e; void add(int u,int v,int f,int c)
{
p[e].u=u;
p[e].v=v;
p[e].f=f;
p[e].val=c;
p[e].next=head[u];
head[u]=e++;
p[e].u=v;
p[e].v=u;
p[e].f=;
p[e].val=-c;
p[e].next=head[v];
head[v]=e++; } bool spfa()
{
int j,k,i;
while(!q.empty()) q.pop();
memset(vis,false,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
memset(pre,-,sizeof(pre));
vis[s]=true;
dis[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(j=head[u]; j!=-; j=p[j].next)
{
int v=p[j].v;
if(p[j].f&&dis[u]+p[j].val<dis[v])
{
dis[v]=dis[u]+p[j].val;
pre[v]=j;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
return dis[t]!=inf;
} int mincost()
{
int ret=,u;
while(spfa())
{
u=pre[t];
ret+=dis[t];
while(u!=-)
{
p[u].f--;
p[u^].f++;
u=pre[p[u].u];
}
}
return ret;
}
int main()
{
int case1;
scanf("%d",&case1);
for(int k=; k<=case1; k++)
{
e=;
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
s=;
t=*n+;
for(int i=; i<=m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b+n,,c);
add(b,a+n,,c);
}
for(int i=; i<=n; i++)
{
add(s,i,,);
add(i+n,t,,);
}
int j;
int ans=mincost();
for(j=head[s]; j!=-; j=p[j].next)
if(p[j].f!=) break;
if(j==-) printf("Case %d: %d\n",k,ans);
else printf("Case %d: NO\n",k);
}
return ;
}

hdu 3435 A new Graph Game的更多相关文章

  1. HDU 3435 A new Graph Game(最小费用流:有向环权值最小覆盖)

    http://acm.hdu.edu.cn/showproblem.php?pid=3435 题意:有n个点和m条边,你可以删去任意条边,使得所有点在一个哈密顿路径上,路径的权值得最小. 思路: 费用 ...

  2. 【刷题】HDU 3435 A new Graph Game

    Problem Description An undirected graph is a graph in which the nodes are connected by undirected ar ...

  3. HDU 3435 A new Graph Game(最小费用最大流)&amp;HDU 3488

    A new Graph Game Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【HDU 3435】 A new Graph Game (KM|费用流)

    A new Graph Game Problem Description An undirected graph is a graph in which the nodes are connected ...

  5. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

  6. HDU 3435 KM A new Graph Game

    和HDU 3488一样的,只不过要判断一下是否有解. #include <iostream> #include <cstdio> #include <cstring> ...

  7. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  8. HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  9. HDU 5631 Rikka with Graph 暴力 并查集

    Rikka with Graph 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5631 Description As we know, Rikka ...

随机推荐

  1. call-template和apply-templates

    对xml模板 来说,name属性是很关键的 call-template /apply-template 的name必须要和模板的name相对应.模板相当于一个函数,可以暂时这么看.而name相当于函数 ...

  2. UVALive 4957 Fake scoreboard

    题意就是有n个队伍和m个题目 给出了每个队伍解决的题目数量 每个题目也给出了被解决的次数 然后求一个方阵. N,Y表示每个队伍是否过了哪个题目. 要求字典序最小. 这题给人的第一反应就是网络流. 虽然 ...

  3. SKView类

    继承自 UIView:UIResponder:NSObject 符合 NSCoding(UIView)UIAppearance(UIView)UIAppearanceContainer(UIView) ...

  4. [Javascript] What is JavaScript Function Currying?

    Currying is a core concept of functional programming and a useful tool for any developer's toolbelt. ...

  5. Dijkstra算法and Floyd算法 HDU 1874 畅通工程续

    Dijkstra算法描述起来比较容易:它是求单源最短路径的,也就是求某一个点到其他各个点的最短路径,大体思想和prim算法差不多,有个数组dis,用来保存源点到其它各个点的距离,刚开始很好办,只需要把 ...

  6. 退出应用 关闭多个Activity

    Activity3 public class Activity3 extends ListActivity {     protected void onCreate(Bundle savedInst ...

  7. 95秀-dialog 进度对话框 实用工具

    工具Util public class DialogUtil {     public static ProgressDialogView progressDialog;     /**      * ...

  8. String对象之间的比较

    public class StringTest { @Test public void test01() { int a = 50; // 基本数据类型比较的是值 int b = 50; System ...

  9. 使用静态资源设置UI信息

    首先建立一个文件存放样式设置(资源字典),所有风格设置都可以这里进行 加入以下代码: <ResourceDictionary xmlns="http://schemas.microso ...

  10. CSS3 用户界面

    CSS3用户界面 在CSS3中,新的用户界面特性包括重设元素尺寸,盒尺寸以及轮廓等. 用户界面属性: resize box-sizing outline-offset 浏览器支持 属性 浏览器支持 r ...