Mining Station on the Sea (hdu 2448 SPFA+KM)
Mining Station on the Sea
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2584 Accepted Submission(s): 780
to s2448eabed mineral resources, the radio signal in the sea is often so weak that not all the mining stations can carry out direct communication. However communication is indispensable, every two mining stations must be able to communicate with each other
(either directly or through other one or more mining stations). To meet the need of transporting the exploited resources up to the land to get put into use, there build n ports correspondently along the coast and every port can communicate with one or more
mining stations directly.
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!
to the link between a mining station and another one, p indicates p edges, each edge indicating the link between a port and a mining station. The following line is n integers, each one indicating one station that one vessel belongs to. Then there follows k
lines, each line including 3 integers a, b and c, indicating the fact that there exists direct communication between mining stations a and b and the distance between them is c. Finally, there follows another p lines, each line including 3 integers d, e and
f, indicating the fact that there exists direct communication between port d and mining station e and the distance between them is f. In addition, mining stations are represented by numbers from 1 to m, and ports 1 to n. Input is terminated by end of file.
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
题意:有m个海上基站。n个港湾。如今有n仅仅船在n个基站里,基站与基站之间有通讯的船才干够走这条路,告诉基站之间的距离,基站与港湾的距离。如今船要回到港湾,一个港湾仅仅能停靠一仅仅船,并且一旦进去就不能出来了。求全部船都回到港湾要走的最短距离之和。
思路:先用最短路求出每一个船的起始点到每一个港湾的最短距离,而且连边,然后求二分图的最小权匹配。用KM算法。费用流也能够做,但我姿势不够优美超时了。。
。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <map>
- #include <stack>
- #include <vector>
- #include <set>
- #include <queue>
- #pragma comment (linker,"/STACK:102400000,102400000")
- #define mod 1000000009
- #define INF 0x3f3f3f3f
- #define pi acos(-1.0)
- #define eps 1e-6
- #define lson rt<<1,l,mid
- #define rson rt<<1|1,mid+1,r
- #define FRE(i,a,b) for(i = a; i <= b; i++)
- #define FREE(i,a,b) for(i = a; i >= b; i--)
- #define FRL(i,a,b) for(i = a; i < b; i++)
- #define FRLL(i,a,b) for(i = a; i > b; i--)
- #define mem(t, v) memset ((t) , v, sizeof(t))
- #define sf(n) scanf("%d", &n)
- #define sff(a,b) scanf("%d %d", &a, &b)
- #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
- #define pf printf
- #define DBG pf("Hi\n")
- typedef long long ll;
- using namespace std;
- const int N=350;
- const int MAXM = 1000000;
- struct Edge{
- int u,v,len,next;
- }edge[MAXM];
- int n,m,k,p,num;
- int dis[N],head[N];
- bool inq[N];
- int nx,ny; //两边的点数
- int g[N][N]; //二分图描写叙述,g赋初值为-INF
- int linker[N],lx[N],ly[N]; //y 中各点匹配状态。x,y中的点的标号
- int slack[N];
- bool visx[N],visy[N];
- bool flag;
- void init()
- {
- num=0;
- memset(head,-1,sizeof(head));
- }
- void addedge(int u,int v,int len)
- {
- edge[num]={u,v,len,head[u]};
- head[u]=num++;
- }
- void SPFA(int s)
- {
- int i,j;
- queue<int>Q;
- memset(inq,false,sizeof(inq));
- memset(dis,INF,sizeof(dis));
- Q.push(s);
- dis[s]=0;
- inq[s]=true;
- while (!Q.empty())
- {
- int u=Q.front();Q.pop();
- inq[u]=false;
- for (int i=head[u];i+1;i=edge[i].next)
- {
- int v=edge[i].v;
- if (dis[v]>dis[u]+edge[i].len)
- {
- dis[v]=dis[u]+edge[i].len;
- if (!inq[v])
- {
- Q.push(v);
- inq[v]=true;
- }
- }
- }
- }
- }
- 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()
- {
- flag=true;
- memset(linker,-1,sizeof(linker));
- memset(ly,0,sizeof(ly));
- for (int i=0;i<nx;i++) //赋初值。lx置为最大值
- {
- 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||g[linker[i]][i]<=-INF) //有的点不能匹配的话return-1
- {
- flag=false;
- continue;
- }
- res+=g[linker[i]][i];
- }
- return res;
- }
- //记得nx和ny初始化!!!。!
- !!
- !
- int start[N];
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
- #endif
- int i,j,u,v,cost;
- while (~scanf("%d%d%d%d",&n,&m,&k,&p))
- {
- init();
- nx=n;
- ny=n;
- for (i=0;i<n+m+10;i++)
- for (j=0;j<n+m+10;j++)
- g[i][j]=-INF;
- for (i=1;i<=n;i++)
- sf(start[i]);
- for (i=0;i<k;i++)
- {
- sfff(u,v,cost);
- addedge(u,v,cost); //网站之间的便能够走多次
- addedge(v,u,cost);
- }
- for (i=0;i<p;i++)
- {
- sfff(u,v,cost);
- addedge(v,u+m,cost); //注意这里是单向边,由于港口仅仅进不出
- }
- for (i=1;i<=n;i++)
- {
- SPFA(start[i]); //SPFA求出每一个船起始位置到港湾的最短距离
- for (j=1;j<=n;j++)
- {
- if (dis[j+m]!=INF)
- g[i-1][j-1]=-dis[j+m];
- // else
- // g[i-1][j-1]=0;
- }
- }
- int ans=KM();
- printf("%d\n",-ans);
- }
- return 0;
- }
Mining Station on the Sea (hdu 2448 SPFA+KM)的更多相关文章
- Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)
Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- 【转载】【最短路Floyd+KM 最佳匹配】hdu 2448 Mining Station on the Sea
Mining Station on the Sea Problem Description The ocean is a treasure house of resources and the dev ...
- HDU-2448 Mining Station on the Sea
先根据不同的起点跑最短路,记录距离,从而建立二分图求最小匹配. 一开始我求最短路的时候我把港口直接加到图中,然后发现进了港口就不能出来了,所以连接港口的边就要从双向边改成单向边…………这也搞得我n和m ...
- Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 本原串(HDU 2197 快速幂)
本原串 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Big Event in HDU(HDU 1171 多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 命运(HDU 2571 简单动态规划)
命运 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- I NEED A OFFER! (hdu 1203 01背包)
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 26. Intellij IDEA 启动项目ClassNotFoundException
转自:https://blog.csdn.net/zhw0596/article/details/81388147 使用Intellij IDEA 的过程中,新创建的项目启动时报 严重: Error ...
- springMVC的一些配置解析
<mvc:annotation-driven /> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> 是一种简写形式,完全可以手 ...
- linux大于2T的磁盘使用GPT分区的方法分享
(parted)表示在parted中输入的命令,其他为自动打印的信息 1.首先类似fdisk一样,先选择要分区的硬盘,此处为/dev/sdb ey: parted /dev/sdb 2.选择了/dev ...
- 由安装两块网卡的linux系统中引起网络不通想到的
由安装两块网卡的linux系统中引起网络不通想到的 一天,小王突然急匆匆的来找我,他说:"我在机子上刚装的redhat怎么老也ping不通服务器,我网卡的驱动都安装了,ping 自己的两块网 ...
- java线程深入学习
一.java中的线程是通过Thread类创建的, //下面是构造函数,一个共同的特点就是:都是调用init()进行创建的 public Thread() { init(null, null, &quo ...
- 如何建立远程桌面连接(XP、Vista、Win7)
如何建立远程桌面连接(XP.Vista.Win7) 要求: 1:对方即你要连的机器必须要允许远程桌面连接,操作系统一般是winXP(单用户)和win2003server(多用户),具体设置:右击我的电 ...
- Copying GC (Part one)
目录 GC复制算法 copy()函数 将传递给自己的参数复制,然后递归复制其孩子 new_obj()函数 执行过程 缺点 Cheney的GC复制算法 copy()函数 执行过程 被隐藏的队列 优缺点 ...
- Windows 一键关闭UAC、防火墙、IE配置脚本
有时候,在环境需求下,需要关闭windows防火墙,UAC,以及IE选项配置. 对不懂电脑来说是比较麻烦的,老是得教他们,关键还记不住…… so,以下脚本就可以解决这个问题 注:脚本 需要右键 以管理 ...
- PostgreSQL递归查询实现树状结构查询
在Postgresql的使用过程中发现了一个非常有意思的功能,就是对于须要相似于树状结构的结果能够使用递归查询实现.比方说我们经常使用的公司部门这样的数据结构.一般我们设计表结构的时候都是相似以下的S ...
- 【v2.x OGE教程 12】 关卡编辑器帮助文档
] 关卡编辑器帮助文档 一.简单介绍 关卡编辑器用于游戏关卡界面元素的可视化编辑,包含元素的位置.尺寸以及其他自己定义属性.通过解析生成的数据文件就可以获取关卡信息,并能随时调整.以降低开发工作量,提 ...