Marriage Match IV

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

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6081    Accepted Submission(s): 1766

Problem Description
Do not sincere non-interference。
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.

So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?

 
Input
The first line is an integer T indicating the case number.(1<=T<=65)
For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.

Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.

At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.

 
Output
Output a line with a integer, means the chances starvae can get at most.
 
Sample Input

3
7 8
1 2 1
1 3 1
2 4 1
3 4 1
4 5 1
4 6 1
5 7 1
6 7 1
1 7

6 7
1 2 1
2 3 1
1 3 3
3 4 1
3 5 1
4 6 1
5 6 1
1 6

2 2
1 2 1
1 2 2
1 2

 
Sample Output
2
1
1
 #include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define maxn 100005
#define MAXN 10005
#define mem(a,b) memset(a,b,sizeof(a))
const int N=;
const int M=;
const int INF=0x3f3f3f3f;
using namespace std;
inline int read()
{
char ch=' ';
int ans=;
while(ch<'' || ch>'')
ch=getchar();
while(ch<='' && ch>='')
{
ans=ans*+ch-'';
ch=getchar();
}
return ans;
}
int n;
vector<pair<int,int> >ve[maxn];
int dis[maxn],dis1[maxn],dis2[maxn],vis[maxn];
int a[maxn],b[maxn],c[maxn];
struct sair{
int pos,len;
friend bool operator<(sair a,sair b){
return a.len>b.len;
}
}; void Dijstra(int s){
for(int i=;i<=n;i++){
dis[i]=INF;
vis[i]=;
}
priority_queue<sair>Q;
sair tmp;
tmp.pos=s,tmp.len=;
Q.push(tmp);
dis[s]=;
int now,Ne,len;
while(!Q.empty()){
tmp=Q.top();
Q.pop();
now=tmp.pos;
if(!vis[now]){
vis[now]=;
for(int i=;i<ve[now].size();i++){
Ne=ve[now][i].first;
len=ve[now][i].second;
if(dis[Ne]>dis[now]+len){
dis[Ne]=dis[now]+len;
tmp.pos=Ne;
tmp.len=dis[Ne];
Q.push(tmp);
}
}
}
}
} struct Edge{
int v,next;
int cap,flow;
}edge[MAXN*];//注意这里要开的够大。。不然WA在这里真的想骂人。。问题是还不报RE。。
int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
int cnt=;//实际存储总边数
void isap_init()
{
cnt=;
memset(pre,-,sizeof(pre));
}
void isap_add(int u,int v,int w)//加边
{
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=;
edge[cnt].next=pre[u];
pre[u]=cnt++;
}
void add(int u,int v,int w){
isap_add(u,v,);
isap_add(v,u,);
}
bool bfs(int s,int t)//其实这个bfs可以融合到下面的迭代里,但是好像是时间要长
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=;
dep[t]=;
queue<int>q;
while(!q.empty())
q.pop();
q.push(t);//从汇点开始反向建层次图
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=pre[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(dep[v]==-&&edge[i^].cap>edge[i^].flow)//注意是从汇点反向bfs,但应该判断正向弧的余量
{
dep[v]=dep[u]+;
gap[dep[v]]++;
q.push(v);
//if(v==sp)//感觉这两句优化加了一般没错,但是有的题可能会错,所以还是注释出来,到时候视情况而定
//break;
}
}
}
return dep[s]!=-;
}
int isap(int s,int t)
{
if(!bfs(s,t))
return ;
memcpy(cur,pre,sizeof(pre));
//for(int i=1;i<=n;i++)
//cout<<"cur "<<cur[i]<<endl;
int u=s;
path[u]=-;
int ans=;
while(dep[s]<n)//迭代寻找增广路
{
if(u==t)
{
int f=INF;
for(int i=path[u];i!=-;i=path[edge[i^].v])//修改找到的增广路
f=min(f,edge[i].cap-edge[i].flow);
for(int i=path[u];i!=-;i=path[edge[i^].v])
{
edge[i].flow+=f;
edge[i^].flow-=f;
}
ans+=f;
u=s;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(dep[v]+==dep[u]&&edge[i].cap-edge[i].flow)
{
cur[u]=path[v]=i;//当前弧优化
flag=true;
break;
}
}
if(flag)
{
u=v;
continue;
}
int x=n;
if(!(--gap[dep[u]]))return ans;//gap优化
for(int i=pre[u];i!=-;i=edge[i].next)
{
if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
{
x=dep[edge[i].v];
cur[u]=i;//常数优化
}
}
dep[u]=x+;
gap[dep[u]]++;
if(u!=s)//当前点没有增广路则后退一个点
u=edge[path[u]^].v;
}
return ans;
} int main(){
int T;
T=read();
while(T--){
int m;
n=read();
m=read();
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=m;i++){
a[i]=read();
b[i]=read();
c[i]=read();
}
int s,t;
s=read();
t=read();
for(int i=;i<=m;i++){
if(a[i]!=b[i])
ve[a[i]].push_back(make_pair(b[i],c[i]));
}
Dijstra(s);
memcpy(dis1,dis,sizeof(dis));
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=m;i++){
if(a[i]!=b[i])
ve[b[i]].push_back(make_pair(a[i],c[i]));
}
Dijstra(t);
memcpy(dis2,dis,sizeof(dis));
isap_init();
for(int i=;i<=m;i++){
if(a[i]!=b[i]&&dis1[a[i]]+dis2[b[i]]+c[i]==dis1[t]){
add(a[i],b[i],);
}
}
int ans=;
ans=isap(s,t);
printf("%d\n",ans);
}
}

最新文章

  1. 【HTML】Html页面跳转的5种方式
  2. FTP多任务下载实现类
  3. Java数据结构和算法之递归
  4. 精通正则表达式(第三版)——Mastering Regular Expressions,3rd Edition——读书笔记1
  5. 三星电视删除USB播放记录
  6. USB枚举过程
  7. Java学习笔记之:Java 定时任务
  8. FFmpeg介绍及参数详细说明
  9. jenkins部署
  10. 【转】SQL多条件模糊查询解决方案-存储过程
  11. jmeter性能测试 套路二
  12. Eclipse常用不常用快捷键
  13. 基于Spring Security OAuth2搭建的Spring Cloud 认证中心
  14. Python小练习
  15. springboot + @KafkaListener 手动提交及消费能力优化
  16. C#_Winfrom将浏览器生成Image
  17. windows快速删除大量文件
  18. 在LaTeX中使用颜色 Using colours in LaTeX
  19. 【Hibernate】Hibernate3.x独立执行时的Failed to load class &amp;quot;org.slf4j.impl.StaticLoggerBinder&amp;quot;错误
  20. DIV+CSS布局重新学习之float/margin/padding

热门文章

  1. ajax异步、同步问题,KindEditor ajax提交内容,ajax提交form表单 解决按两次的问题
  2. 2015ACM-ICPC长春E题(hdu5531)题解
  3. 哈希与位图(Hash and BitMap)
  4. event事件传播规则
  5. 暴搜 - Codeforces Round #327 (Div. 2) E. Three States
  6. Notepad++配置c++编译环境
  7. systemctl管理系统配置、服务
  8. RabbitMQ 端口号解析
  9. 11.使用ForwardAction实现页面屏蔽。
  10. 使用.htaccess文件