HDU 3599 War(最短路+最大流)

War

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1746    Accepted Submission(s): 414


Problem Description
In the war between Anihc and Bandzi, Anihc has won Bangzi. At that time, Lvbin, the king of Anihc, want to start from Bangzi to beat Nebir across the channel between them. He let his army get to there as soon as possible, and there located many islands which can be used to have a break. In order to save time, the king forbid the army getting through the same pass for more than one time, but they can reach the same island for as many as times they want.
Yunfeng, the General of the army, must tell how many optimal ship routes are there to the king as soon as possible, or he will be killed. Now he asks for your help. You must help Yunfeng to save his life.
He tells you that there are N island. The islands are numbered from 1 to N(1 is Bangzi and N is Nebir, others are many islands). And there are many ways, each way contain the islands number U and V and the length W. Please output your answer.
 

Input
The first line in the input file contains a single integer number T means the case number. 
Each case contains a number N (N<=1500) means the number of the islands. 
And then many lines follow. Each line contains three numbers: U V W (W<10000), means that the distance between island U and V is W. The input of ways are terminated by “ 0 0 0 ”.
 

Output
Print the number of the optimal ship routes are there after each case in a line.
 

Sample Input
 
  
1 6 1 2 1 3 2 1 3 4 1 1 3 2 4 2 2 4 5 1 5 6 1 4 6 2 0 0 0
 

Sample Output
 
  
2

题解:
求不重复最短路的条数,把最短路存下来建边跑最大流即可。
注意n==1时要特判。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1505;
const int inf=1e9;
int head[maxn],d[maxn],tol,level[maxn],iter[maxn];
int from[maxn*maxn],to[maxn*maxn],cost[maxn*maxn];
bool vis[maxn];
struct edge
{
    int to,cost,next;
}rode[maxn*maxn];
struct node
{
    int to,cap,rev;
};
vector<node>G[maxn];
void add(int a,int b,int c)
{
    rode[tol].to=b;
    rode[tol].cost=c;
    rode[tol].next=head[a];
    head[a]=tol++;
}
void spfa()
{
    for(int i=0;i<maxn;i++)
        d[i]=1e9;
    memset(vis,0,sizeof(vis));
    d[1]=0;
    queue<int>P;P.push(1);
    while(!P.empty())
    {
        int v=P.front();P.pop();
        vis[v]=0;
        for(int i=head[v];i!=-1;i=rode[i].next)
        {
            edge e=rode[i];
            if(d[e.to]>d[v]+e.cost)
            {
                d[e.to]=d[v]+e.cost;
                if(!vis[e.to])
                {
                    P.push(e.to);
                    vis[e.to]=1;
                }
            }
        }
    }
}
void add_edge(int from,int to,int cap)
{
    G[from].push_back((node){to,cap,G[to].size()});
    G[to].push_back((node){from,0,G[from].size()-1});
}
void ADD()
{
     for(int i=0;i<maxn;i++)
        G[i].clear();
    for(int i=0; ;i++)
    {
        int a=from[i],b=to[i],c=cost[i];
        if(a==0&&b==0&&c==0)break;
        if(d[a]-d[b]==c)
            add_edge(b,a,1);
        else if(d[b]-d[a]==c)
            add_edge(a,b,1);
    }
}
void bfs(int s)
{
    memset(level,-1,sizeof(level));
    level[s]=0;
    queue<int>P;
    P.push(s);
    while(!P.empty())
    {
        int v=P.front();P.pop();
        for(int i=0;i<G[v].size();i++)
        {
            node e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                P.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t)return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        node &e=G[v][i];
        if(e.cap>0&&level[e.to]>level[v])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int v,int t)
{
    ADD();
    int flow=0;
    while(1)
    {
        bfs(v);
        if(level[t]<0)return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(v,t,inf))>0)flow+=f;
    }
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        tol=0;
        memset(head,-1,sizeof(head));
        int n;scanf("%d",&n);
        for(int i=0; ;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            from[i]=a;to[i]=b;cost[i]=c;
            if(a==0&&b==0&&c==0)break;
            add(a,b,c);add(b,a,c);
        }
        spfa();
        if(n==1)
        {
            printf("0\n");
            continue;
        }
        printf("%d\n",max_flow(1,n));//若n==1则1到1无法跑最大流
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/80281786