1 条题解
-
0
C++ :
#include<ctime> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<cassert> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #include<sstream> #include<climits> #define X first #define Y second #define DB double #define lc now<<1 #define rc now<<1|1 #define MP make_pair #define LL long long #define pb push_back #define sqr(_) ((_)*(_)) #define INF 0x3f3f3f3f #define pii pair<int,int> #define pdd pair<DB,DB> #define ull unsigned LL #define DEBUG(...) fprintf(stderr,__VA_ARGS__) using namespace std; template<typename T>void Read(T& x) { x=0;int flag=0,sgn=1;char c; while(c=getchar()) { if(c=='-')sgn=-1; else if(c>='0'&&c<='9')x*=10,x+=c-'0',flag=1; else if(flag)break; } x*=sgn; } const int MAXM=40009,MAXN=320; int dis[160][160],first[MAXN],nnext[MAXM],to[MAXM],w[MAXM],n,m,k,e=0; void add(int a,int b,int c) { nnext[e]=first[a];first[a]=e;to[e]=b;w[e]=c;++e; nnext[e]=first[b];first[b]=e;to[e]=a;w[e]=c;++e; } struct Edge{ int u,v,cap,flow,cost; Edge(int u=0,int v=0,int cap=0,int flow=0,int cost=0): u(u),v(v),cap(cap),flow(flow),cost(cost){} }; struct Mincost{ vector<int> G[MAXN]; vector<Edge> edges; int p[MAXN],d[MAXN],S,T,inq[MAXN],a[MAXN]; void add(int u,int v,int cap,int cost) { edges.pb(Edge(u,v,cap,0,cost)); edges.pb(Edge(v,u,0,0,-cost)); int K=edges.size(); G[u].pb(K-2); G[v].pb(K-1); } bool SPFA(int& flow,int& cost) { memset(p,0,sizeof(p)); memset(inq,0,sizeof(inq)); memset(a,INF,sizeof(a)); memset(d,INF,sizeof(d)); queue<int> q; q.push(S); inq[S]=1; d[S]=0; while(!q.empty()) { int u=q.front();q.pop(); inq[u]=0; for(int i=0;i<G[u].size();i++) { Edge& e=edges[G[u][i]]; if(e.cap>e.flow&&d[e.v]>d[u]+e.cost) { d[e.v]=d[u]+e.cost; p[e.v]=G[u][i]; a[e.v]=min(a[e.v],e.cap-e.flow); if(!inq[e.v]) q.push(e.v), inq[e.v]=1; } } } if(a[T]==INF)return 0; flow+=a[T]; cost+=a[T]*d[T]; int U=T; while(U!=S) { edges[p[U]].flow+=a[T]; edges[p[U]^1].flow-=a[T]; U=edges[p[U]].u; } return 1; } int mincost() { int flow=0,cost=0; while(SPFA(flow,cost)); return cost; } }Graph; int main() { #ifndef ONLINE_JUDGE freopen("w.in","r",stdin); freopen("w.out","w",stdout); #endif Read(n);Read(m);Read(k); memset(dis,INF,sizeof(dis)); for(int i=1;i<=m;i++) { int a,b,l; Read(a);Read(b);Read(l); add(a,b,l); dis[a][b]=min(dis[a][b],l); dis[b][a]=min(dis[b][a],l); } for(int i=0;i<=n;i++) dis[i][i]=0; for(int K=0;K<=n;K++) { for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) { if(K>j&&K>i)continue; dis[i][j]=min(dis[i][K]+dis[K][j],dis[i][j]); } } for(int i=0;i<=n;i++) for(int j=i+1;j<=n;j++) if(dis[i][j]!=INF&&i!=j) Graph.add(i,j+n,1,dis[i][j]); Graph.S=2*n+1,Graph.T=2*n+2; Graph.add(Graph.S,0,k,0); for(int i=1;i<=n;i++) Graph.add(Graph.S,i,1,0); for(int i=1;i<=n;i++) Graph.add(i+n,Graph.T,1,0); cout<<Graph.mincost()<<endl; }
- 1
信息
- ID
- 983
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者