您的位置:首页 > 其它

HDU 3631 floyd

2016-12-02 23:16 288 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=3631

题意:给定一个有重边的有向带权图,n个点,m条边,q个询问。每个询问分两种,0 x,表示标记x点,1 x y,输出x到y的最短路且经过的都是标记过的点,当x或y为未标记的点时输出ERROR。n<=300,m<=1e5。

思路:运用floyd的原理,对点进行标记的时候跑一遍floyd,总的时间复杂度O(n^3)。

#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 305;
const int maxm = 100005;

int maze[maxn][maxn];
bool vis[maxn];

int n,m,q;

int main(){
int ca = 0;
while(scanf("%d%d%d",&n,&m,&q) == 3 && (n||m||q)){
memset(maze,0x3f,sizeof(maze));
memset(vis,0,sizeof(vis));
if(ca) printf("\n");
printf("Case %d:\n",++ca);
int u,v,w;
for(int i = 0; i < m; ++i){
scanf("%d%d%d",&u,&v,&w);
if(w < maze[u][v]){
maze[u][v] = w;
}
}
for(int i = 0; i < n; ++i) maze[i][i] = 0;
for(int i = 0; i < q; ++i){
int c;
scanf("%d",&c);
if(c == 0){
int temp;
scanf("%d",&temp);
if(!vis[temp]){
vis[temp] = 1;
for(int x = 0; x < n; ++x){
for(int y = 0; y < n; ++y){
maze[x][y] = min(maze[x][y],maze[x][temp]+maze[temp][y]);
}
}
}
else printf("ERROR! At point %d\n",temp);
}
else if(c == 1){
int a,b;
scanf("%d%d",&a,&b);
if(!vis[a] || !vis[b]){
printf("ERROR! At path %d to %d\n",a,b);
}
else if(maze[a][b] < INF) printf("%d\n",maze[a][b]);
else printf("No such path\n");
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: