记录编号 | 192690 | 评测结果 | AAAAAAAAAA | ||
---|---|---|---|---|---|
题目名称 | [SGU U252]铁路网 | 最终得分 | 100 | ||
用户昵称 | 是否通过 | 通过 | |||
代码语言 | C++ | 运行时间 | 0.013 s | ||
提交时间 | 2015-10-12 16:52:58 | 内存使用 | 0.55 MiB | ||
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<deque> using namespace std; const int SIZEN=310,maxn=0x7fffffff/2; int N,M; deque<int> s[SIZEN]; int sw[SIZEN]={0},key[SIZEN],ans=0,tot,S,T,nowflow; class miku { public: int fr,to,cap,flow,cost; miku(){} miku(int a,int b,int c,int d,int e) { fr=a,to=b,cap=c,flow=d,cost=e; } }E[16*SIZEN]; void add(int fr,int to,int cost) { s[fr].push_back(tot); E[tot++]=miku(fr,to,1,0,cost); s[to].push_back(tot); E[tot++]=miku(to,fr,0,0,-cost); } void read() { scanf("%d%d",&N,&M); int a,b,c; S=0,T=2*N+1; for(int i=1;i<=M;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b+N,c); } for(int i=1;i<=N;i++) add(S,i,0),add(i+N,T,0); } bool SPFA() { deque<int> Q; Q.push_back(S); for(int i=0;i<=T;i++) sw[i]=maxn,key[i]=-1; bool inq[SIZEN]={0}; sw[0]=0;inq[S]=1; while(!Q.empty()) { int x=Q.front();inq[x]=0;Q.pop_front(); for(int i=0;i<s[x].size();i++) { miku &v=E[s[x][i]]; if(v.cap<=v.flow) continue; if(sw[x]+v.cost<sw[v.to]) { sw[v.to]=sw[x]+v.cost; key[v.to]=s[x][i]; if(!inq[v.to]) { Q.push_back(v.to); inq[v.to]=1; } } } } if(sw[T]==maxn) return 0; return 1; } void dfs() { int tem=T; nowflow++; ans+=sw[T]; while(tem!=0) { int t=key[tem]; E[t].flow+=1; E[t^1].flow-=1; tem=E[t].fr; } } void work() { nowflow=0; while(SPFA()) dfs(); printf("%d %d",N-nowflow,ans); } int main() { freopen("railwaycommunication.in","r",stdin); freopen("railwaycommunication.out","w",stdout); read(); work(); return 0; }