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? 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
題意:在城市A的男孩想去跟城市B的女孩約合,不過他去城市B必須走A->B最短路,並且走過的路不可以再走,問他最多能看這個女孩多少次。 解法:找到A -> B的最短路徑的邊,將其他邊去掉,將最短路上的邊的最大流量設為1,跑一遍最大流,可以得到A -> B的最大流量。。。。 先從A->B做一次spfa,然後再反向建圖從 B->A做一次spfa。然後遍歷原圖每一條邊e(u,v),若滿足 dis1[u] + dis2[v] + w(u,v) == dis1[B]。 則說明邊 e(u,v) 屬於A - > B 的最短路徑。 (dis1[u] 從A->u的最短路) (dis2[v] 從B->v的最短路) 這道題用dinic的話應該用個好一點的板子,我用的那個模板超時了。。。。 也可以用sap,sap比dinic快好多
spfa + dinic
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MAXN = 1005;
const int N = 2005;
const int M = N * N;
struct Edge{
int u,v, w, nxt;
};
int s,t;
int n, m, cnt1,cnt2;
Edge ss[MAXN * MAXN],se[MAXN * MAXN];
int dis1[MAXN],dis2[MAXN],head1[MAXN],head2[MAXN];
bool vis[MAXN];
void init(){
cnt1 = cnt2 = 0;
memset(head1,-1,sizeof(head1));
memset(head2,-1,sizeof(head2));
}
void addEdge(Edge e[],int hh[],int &tt,int u, int v, int w){
e[tt].u = u;
e[tt].v = v;
e[tt].w = w;
e[tt].nxt = hh[u];
hh[u] = tt++;
}
struct Dinic{
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];
void init() {
ec = 0;
memset(first, -1, sizeof(first));
}
void addEdge(int a,int b,int c) {
to[ec] = b;
v[ec] = c;
Next[ec] = first[a];
first[a] = ec++;
to[ec] = a;
v[ec] = 0;
Next[ec] = first[b];
first[b] = ec++;
}
int BFS() {
int kid, now, f = 0, r = 1, i;
memset(lev, 0, sizeof(lev));
que[0] = s, lev[s] = 1;
while (f < r) {
now = que[f++];
for (i = first[now]; i != -1; i = Next[i]) {
kid = to[i];
if (!lev[kid] && v[i]) {
lev[kid] = lev[now] + 1;
if (kid == t) return 1;
que[r++] = kid;
}
}
}
return 0;
}
int DFS(int now, int sum) {
int kid, flow, rt = 0;
if (now == t) return sum;
for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
head[now] = i;
kid = to[i];
if (lev[kid] == lev[now] + 1 && v[i]) {
flow = DFS(kid, min(sum - rt, v[i]));
if (flow) {
v[i] -= flow;
v[i^1] += flow;
rt += flow;
} else lev[kid] = -1;
}
}
return rt;
}
int dinic() {
int ans = 0;
while (BFS()) {
for (int i = 0; i <= n; i++) {
head[i] = first[i];
}
ans += DFS(s, inf);
}
return ans;
}
}din;
void spfa(int s,int dd[],int hh[],Edge e[])
{
memset(vis,false,sizeof(vis));
for(int i = 1;i <= n;++i){
dd[i] = inf;
}
queue<int>que;
vis[s] = true;
dd[s] = 0;
que.push(s);
while(!que.empty())
{
int tmp = que.front();
que.pop();
vis[tmp] = false;
//cout << hh[tmp] << endl;
for(int i = hh[tmp];i != -1;i = e[i].nxt)
{
int to = e[i].v;
int val = e[i].w;
//cout << to << " " << val << endl;
if(dd[tmp] + val < dd[to]){
dd[to] = dd[tmp] + val;
if(!vis[to]){
vis[to] = true;
que.push(to);
}
}
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
init();
for(int i = 0;i < m;++i){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
if(u == v) continue;
addEdge(ss,head1,cnt1,u,v,w);
addEdge(se,head2,cnt2,v,u,w);
}
scanf("%d %d",&s,&t);
spfa(s,dis1,head1,ss);
spfa(t,dis2,head2,se);
// for(int i = 1;i <= n;++i){
// printf("%d ",dis1[i]);
// }
// printf("\n");
// for(int i = 1;i <= n;++i){
// printf("%d ",dis2[i]);
// }
// printf("\n");
din.init();
for(int i = 0;i < cnt1;++i){
if(dis1[ss[i].u] + dis2[ss[i].v] + ss[i].w == dis1[t]){
din.addEdge(ss[i].u,ss[i].v,1);
//addEdge(edge,head,ecnt,ss[i].v,ss[i].u,0);
//cout << node[i].u << " " << node[i].v << endl;
}
}
printf("%d\n",din.dinic());
}
}