ZOJ 2760 How Many Shortest Path(最短路径计数)

87 篇文章 0 订阅

How Many Shortest Path

Time Limit: 10 Seconds       Memory Limit: 32768 KB

Given a weighted directed graph, we define the shortest path as the path who has the smallest length among all the path connecting the source vertex to the target vertex. And if two path is said to be non-overlapping, it means that the two path has no common edge. So, given a weighted directed graph, a source vertex and a target vertex, we are interested in how many non-overlapping shortest path could we find out at most.

Input

Input consists of multiple test cases. The first line of each test case, there is an integer number N (1<=N<=100), which is the number of the vertices. Then follows an N * N matrix, represents the directed graph. Each element of the matrix is either non-negative integer, denotes the length of the edge, or -1, which means there is no edge. At the last, the test case ends with two integer numbers S and T (0<=S, T<=N-1), that is, the starting and ending points. Process to the end of the file.

Output

For each test case, output one line, the number of the the non-overlapping shortest path that we can find at most, or "inf" (without quote), if the starting point meets with the ending.

Sample Input

4
0 1 1 -1
-1 0 1 1
-1 -1 0 1
-1 -1 -1 0
0 3
5
0 1 1 -1 -1
-1 0 1 1 -1
-1 -1 0 1 -1
-1 -1 -1 0 1
-1 -1 -1 -1 0
0 4

Sample Output

2
1


题目大意:

    给定一个有向图,求不重叠的最大最短路数。


解题思路:

    首先求不相交的最大路径数,我们可以直接把有向图所有边容量定为1,然后跑最大流得。题目要求最大最短路数,如果这个图上只有组成最短路的边,我们就可以用刚才的方法求得答案。于是我们就可以先跑一边最短路,然后把不满足dis[u]+G[u][v]==dis[v]的边全部删掉。这样就可以在这个图上求解答案了。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXV=100+3;

struct Edge
{
    int to,cap,rev;
    Edge(int t,int c,int r):to(t),cap(c),rev(r){}
};

int V,S,T,dis[MAXV];
int G[MAXV][MAXV];//原图
bool vis[MAXV];
vector<Edge> G1[MAXV];//最短路组成的图
int level[MAXV];
int iter[MAXV];

void init()
{
    for(int i=0;i<V;++i)
        G1[i].clear();
}

void add_edge(int from,int to,int cap)
{
    G1[from].push_back(Edge(to,cap,G1[to].size()));
    G1[to].push_back(Edge(from,0,G1[from].size()-1));
}

void bfs(int s)
{
    mem(level,-1);
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty())
    {
        int v=que.front(); que.pop();
        for(int i=0;i<G1[v].size();++i)
        {
            Edge &e=G1[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)
{
    if(v==t)
        return f;
    for(int &i=iter[v];i<G1[v].size();++i)
    {
        Edge &e=G1[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G1[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int dinic(int s,int t)
{
    int flow=0;
    for(;;)
    {
        bfs(s);
        if(level[t]<0)
            return flow;
        mem(iter,0);
        int f;
        while((f=dfs(s,t,INF))>0)
            flow+=f;
    }
}

void spfa()
{
    for(int i=0;i<V;++i)
    {
        vis[i]=false;
        dis[i]=INF;
    }
    vis[S]=true;
    dis[S]=0;
    queue<int> que;
    que.push(S);
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        vis[u]=false;
        for(int v=0;v<V;++v)
        {
            if(G[u][v]==INF)
                continue;
            if(dis[v]>dis[u]+G[u][v])
            {
                dis[v]=dis[u]+G[u][v];
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d",&V))
    {
        init();
        for(int i=0;i<V;++i)
            for(int j=0;j<V;++j)
            {
                scanf("%d",&G[i][j]);
                if(G[i][j]==-1||i==j)
                    G[i][j]=INF;
            }
        scanf("%d%d",&S,&T);
        if(S==T)
        {
            puts("inf");
            continue;
        }
        spfa();
        for(int i=0;i<V;++i)
            for(int j=0;j<V;++j)
                if(dis[i]!=INF&&dis[j]!=INF&&G[i][j]!=INF&&dis[i]+G[i][j]==dis[j])//选择组成最短路的边
                {
                    add_edge(i, j, 1);
                }
        printf("%d\n",dinic(S,T));
    }
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题是一个典型的搜索问题,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来解决。以下是使用DFS的代码实现: ```c++ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 20; const int MAXM = 20; int n, m, sx, sy, ex, ey; char maze[MAXN][MAXM]; // 迷宫 int vis[MAXN][MAXM]; // 标记数组 int dx[] = {0, 0, 1, -1}; // 方向数组 int dy[] = {1, -1, 0, 0}; void dfs(int x, int y) { if (x == ex && y == ey) { // 到达终点 printf("(%d,%d)", x, y); return; } for (int i = 0; i < 4; i++) { // 依次尝试四个方向 int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && !vis[nx][ny]) { vis[nx][ny] = 1; // 标记已访问 printf("(%d,%d)->", x, y); dfs(nx, ny); return; } } } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'S') { sx = i; sy = j; } else if (maze[i][j] == 'T') { ex = i; ey = j; } } } vis[sx][sy] = 1; dfs(sx, sy); printf("\n"); } return 0; } ``` 代码实现中,使用了一个标记数组 `vis` 来标记每个位置是否已经被访问过,避免走重复的路线。使用DFS的时候,每次从当前位置依次尝试四个方向,如果某个方向可以走,则标记该位置已经被访问过,并输出当前位置的坐标,然后递归进入下一个位置。如果当前位置是终点,则直接输出并返回。 在输出路径的时候,由于是递归调用,所以输出的路径是反向的,需要将其反转过来,即从终点往起点遍历输出。 需要注意的是,题目中要求输出的路径是 `(x1,y1)->(x2,y2)->...->(xn,yn)` 的形式,每个坐标之间用 `->` 连接。所以在输出的时候需要特别处理第一个坐标和最后一个坐标的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值