zoj 2760 How Many Shortest Path

好早以前就见了,一直也没做。

昨天才做过一个类似的,今天不想做题。。。看会升刚过这个。。。就把这个A了算了。

给出邻接矩阵,求从S到T有多少条不相重叠的最短路。

太坑了,输入的矩阵对角线不一定都是0 = =。。。WA死我了

建图直接和上题一样,不多说了。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=s; i<t; i++)
#define BUG puts("here!!!")
#define STOP system("pause")

using namespace std;

const int MAX = 110;
const int INF = 2100000000;
int a[MAX][MAX];
int b[MAX][MAX];

class Arc{
public:
    int to,w;
    Arc *next,*anti;
    Arc *ass( int tt,int ww,Arc* &b ){
        to = tt;w = ww;next = b;b = this;return this;
    }
};
template< int Vsize, int Esize >
class Network{
private:
    Arc A[ 2*Esize+10 ],*biao[ Vsize ];
    int countt,d[ Vsize ],S,T,N;

    int bfs( void ){
    	queue<int> q;
    	fill( d,d+Vsize,-1 );
    	d[ S ] = 0;
    	q.push( S );
    	while( !q.empty() ){
    		int u = q.front();q.pop();
    		for(Arc *p=biao[u];p!=NULL;p=p->next)
    			if( d[p->to] == -1 && p->w > 0 )
    				d[p->to] = d[u]+1,q.push(p->to);
    	}
    	return d[T] != -1;
    }
    int dinic( int u,int sum ){
    	int f,o;
    	if( u == T ) return sum;
    	o = sum;
    	for(Arc*p=biao[u];p!=NULL&∑p=p->next)
    		if( d[p->to] == d[u]+1 && p->w > 0 ){
    			f = dinic(p->to,min(p->w,sum));
    			p->w -= f;p->anti->w += f;
    			sum -= f;
    		}
    	return o-sum;
    }
public:
    Network( void ) {}
    Network( int n ) { init(n) ;}

    void init( int n ){
        countt = 0;N = n;
        for( int i = 0; i <= n; i++ )
            biao[i] = NULL;
    }
    void add( int from,int to,int w ){
    	Arc *p1 = A[countt++].ass( to,w ,biao[from]);
    	Arc *p2 = A[countt++].ass( from,0,biao[ to] );
    	p1->anti = p2; p2->anti = p1;
    }
    int MaxFlowDinic( int s, int t ){
        S = s;T = t;
        int total = 0;
        while( bfs() )
            total += dinic( S,INF );
        return total;
    }
};

Network<MAX,MAX*MAX> net;

void Floyd(int n)
{
	FOR(i, 0, n)
		FOR(k, 0, n)
			FOR(j, 0, n)
				if( a[k][i] != -1 && a[i][j] != -1 )
					if( a[k][j] == -1 )
						a[k][j] = a[k][i] + a[i][j];
					else
						a[k][j] = min(a[k][j], a[k][i] + a[i][j]);
}

int solve(int n,int s,int t)
{
	memcpy(b, a, sizeof(a));
	Floyd(n);
	FOR(i, 0, n)
	{
		if( a[s][i] == -1 ) continue;
		FOR(k, 0, n)
		{
			if( b[i][k] != -1 && a[s][k] != -1 && a[s][i] + b[i][k] == a[s][k] )
				net.add(i, k, 1);
		}
	}
	return net.MaxFlowSap(s, t);
}

int main()
{
	int n, s, t;
	
	while( ~scanf("%d", &n) )
	{
		net.init(n);
		FOR(i, 0, n)
			FOR(k, 0, n)
			{
				scanf("%d", &a[i][k]);
				if( i == k )
					a[i][k] = 0; // 坑姐啊!! 
			}
			
		scanf("%d%d", &s, &t);
		
		if( s == t )
		{
			puts("inf");
			continue;
		}
		
		int ans = solve(n, s, t);
		
		printf("%d\n", ans);
	}

return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值