2014
03-23

# Marriage Match IV

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?

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.

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.

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

2
1
1

#include<iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
const int N=1100;
const int inf=0x3fffffff;
using namespace std;
struct edge
{
int st,ed,w,next;
}E[100100];
struct Edge
{
int st,ed,flow,next;
}e[300100];
{
E[tot].st=x;E[tot].ed=y;E[tot].w=w;E[tot].next=first[x];first[x]=tot++;
}
{
}
void bfs()
{
queue<int >Q;
int i,v,u;
bool vis[N];
for(i=1;i<=n;i++)
{
cost[i]=inf;vis[i]=false;
}
cost[start]=0;vis[1]=true;
Q.push(start);
while(!Q.empty())
{
u=Q.front();
Q.pop();
vis[u]=false;
for(i=first[u];i!=-1;i=E[i].next)
{
v=E[i].ed;
if(cost[v]>cost[u]+E[i].w)
{
cost[v]=cost[u]+E[i].w;
if(!vis[v])
{
Q.push(v);
vis[v]=true;
}
}
}
}
}
void makemap()
{
int i,j;
num=0;
for(i=1;i<=n;i++)
{
for(j=first[i];j!=-1;j=E[j].next)
{
if(cost[i]+E[j].w==cost[E[j].ed])//可走的边
{
}
}
}
}
int dfs(int u,int minflow)
{
if(u==end)return minflow;
int i,v,f,min_dis=ans-1,flow=0;
{
v=e[i].ed;
if(e[i].flow<=0)continue;
if(dis[v]+1==dis[u])
{
f=dfs(v,e[i].flow>minflow-flow?minflow-flow:e[i].flow);
e[i].flow-=f;
e[i^1].flow+=f;
flow+=f;
if(flow==minflow)break;
if(dis[start]>=ans)return flow;
}
min_dis=min_dis>dis[v]?dis[v]:min_dis;
}
if(flow==0)
{
if(--gap[dis[u]]==0)
dis[start]=ans;
dis[u]=min_dis+1;
gap[dis[u]]++;
}
return flow;
}
int isap()
{
int maxflow=0;
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
gap[0]=ans;
while(dis[start]<ans)
maxflow+=dfs(start,inf);
return maxflow;
}
int main()
{
int i,m,k,t,x,y,w;
scanf("%d",&t);
while(t--)
{
memset(first,-1,sizeof(first));
tot=0;
scanf("%d%d",&n,&m);
for(k=0,i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&w);
if(x==y)continue;
}
scanf("%d%d",&start,&end);
ans=n;
bfs();
makemap();
printf("%d\n",isap());
}
return 0;
}

1. 嗯 分析得很到位，确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样：push时，比较要push的elem和辅助栈的栈顶，elem<=min.top()，则min.push(elem).否则只要push（elem）就好。在pop的时候，比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();}，否则{stack.pop();}.