Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3081 Accepted Submission(s): 932
Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly.
The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal.
Notice that once the ship entered the port, it will not come out!
3 5 5 6 1 2 4 1 3 3 1 4 4 1 5 5 2 5 3 2 4 3 1 1 5 1 5 3 2 5 3 2 4 6 3 1 4 3 2 2
13
/*
* Author: ktmzgl
* Created Time: 2016/10/10 16:29:52
* File Name: F:\Vim\hdu_2448_km+shortestway.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <time.h>
using namespace std;
const int maxint = -1u>>1;
using namespace std;
const int N=310;
const int INF=0x3f3f3f3f;
int nx,ny;//两边的点数
int g[N][N];//二分图描述
int w[N][N];
int linker[N],lx[N],ly[N];//y中各点匹配状态,x,y中的点标号
int slack[N];
bool visx[N],visy[N];
int n,m,k,p;
int st[N];
int v[N];
int d[N];
void dij(int s,int n)
{
memset(v, 0, sizeof(v));//矩阵存储
for(int i = 0; i < n; i++) d[i] = (i==s ? 0 : INF);
for(int i = 0; i < n; i++) {
int x, m = INF;
for(int y = 0; y < n; y++) if(!v[y] && d[y]<=m) m = d[x=y];
v[x] = 1;
for(int y = 0; y < n; y++) d[y] = min(d[y], d[x] + w[x][y]);
}
}
/* KM算法
* 复杂度O(nx*nx*ny)
* 求最大权匹配
* 若求最小权匹配,可将权值取相反数,结果取相反数
* 点的编号从0开始
*/
bool DFS(int x)
{
visx[x] = true;
for(int y = 0; y < ny; y++)
{
if(visy[y])continue;
int tmp = lx[x] + ly[y] - g[x][y];
if(tmp == 0)
{
visy[y] = true;
if(linker[y] == -1 || DFS(linker[y]))
{
linker[y] = x;
return true;
}
}
else if(slack[y] > tmp)
slack[y] = tmp;
}
return false;
}
int KM()
{
memset(linker,-1,sizeof(linker));
memset(ly,0,sizeof(ly));
for(int i = 0;i < nx;i++)
{
lx[i] = -INF;
for(int j = 0;j < ny;j++)
if(g[i][j] > lx[i])
lx[i] = g[i][j];
}
for(int x = 0;x < nx;x++)
{
for(int i = 0;i < ny;i++)
slack[i] = INF;
while(true)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(DFS(x))break;
int d = INF;
for(int i = 0;i < ny;i++)
if(!visy[i] && d > slack[i])
d = slack[i];
for(int i = 0;i < nx;i++)
if(visx[i])
lx[i] -= d;
for(int i = 0;i < ny;i++)
{
if(visy[i])ly[i] += d;
else slack[i] -= d;
}
}
}
int res = 0;
for(int i = 0;i < ny;i++)
if(linker[i] != -1)
res += g[linker[i]][i];
return res;
}
int main()
{
while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d",&st[i]);
st[i]--;
}
memset(w,INF,sizeof(w));
memset(g,0,sizeof(g));
for(int i=0;i<k;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
w[x-1][y-1]=z;
w[y-1][x-1]=z;
}
for(int i=0;i<p;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
w[y-1][x-1+m]=z;
}
for(int i=0;i<n;i++)
{
dij(st[i],n+m);
/*cout<<"start"<<st[i]<<endl;
for(int k=0;k<m+n;k++)
{
cout<<k<<" "<<d[k]<<endl;
}*/
for(int j=m;j<m+n;j++)
{
if(d[j]<INF)
g[i][j-m]=-d[j];
}
}
nx=n,ny=n;
cout<<-KM()<<endl;
}
return 0;
}
2227




被折叠的 条评论
为什么被折叠?



