hdu 3631 Shortest Path (floyd)
2016-07-27 09:44
302 查看
题意:给出n个点,m条边,q个操作。对两个点的路径不得经过未标记的点。操作可以标记点或者询问两个点之间的距离,若操作非法则返回指定语句,否则执行操作。
作为中间点,一个点一个点插入,理解Floyd的过程即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 330
#define INF 0x7ffffff
using namespace std;
int mp
,v
,n,m,q;
void floyd(int k)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
int main()
{
int kase=0;
while(~scanf("%d%d%d",&n,&m,&q)&&(n||m||q))
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i==j) mp[i][j]=0;
else mp[i][j]=INF;
memset(v,0,sizeof(v));
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(mp[u][v]>w) mp[u][v]=w;
}
if(kase) cout<<endl;
printf("Case %d:\n",++kase);
for(int i=0;i<q;i++)
{
int t,x,y;
scanf("%d%d",&t,&x);
if(!t)
{
if(v[x])
{
printf("ERROR! At point %d\n",x);
continue;
}
v[x]=1;
floyd(x);
}
else
{
scanf("%d",&y);
if(v[x]==0||v[y]==0) printf("ERROR! At path %d to %d\n",x,y);
else if(mp[x][y]>=INF) cout<<"No such path"<<endl;
else cout<<mp[x][y]<<endl;
}
}
}
}
作为中间点,一个点一个点插入,理解Floyd的过程即可。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 330
#define INF 0x7ffffff
using namespace std;
int mp
,v
,n,m,q;
void floyd(int k)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
int main()
{
int kase=0;
while(~scanf("%d%d%d",&n,&m,&q)&&(n||m||q))
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i==j) mp[i][j]=0;
else mp[i][j]=INF;
memset(v,0,sizeof(v));
for(int i=0;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(mp[u][v]>w) mp[u][v]=w;
}
if(kase) cout<<endl;
printf("Case %d:\n",++kase);
for(int i=0;i<q;i++)
{
int t,x,y;
scanf("%d%d",&t,&x);
if(!t)
{
if(v[x])
{
printf("ERROR! At point %d\n",x);
continue;
}
v[x]=1;
floyd(x);
}
else
{
scanf("%d",&y);
if(v[x]==0||v[y]==0) printf("ERROR! At path %d to %d\n",x,y);
else if(mp[x][y]>=INF) cout<<"No such path"<<endl;
else cout<<mp[x][y]<<endl;
}
}
}
}
相关文章推荐
- CAD做好的图如何进行坐标转换
- Qt读写ini文件
- 项目中遇到的问题(笔记)
- linux一些实用小技巧
- 工作计划
- Android学习历程——高德地图学习开发之显示地图,定位
- HDU 1827 3072强联通分量
- M1卡的工作原理
- linux shell数据重定向(输入重定向与输出重定向)详细分析
- Vacations
- publishing failed with multiple errors resource is out of sync with the file system--转
- 数据结构上机测试1:顺序表的应用
- Android封装支付宝支付
- tony的js学习笔记--基础知识(随时更新)
- 欢迎使用CSDN-markdown编辑器
- 同一个Thread启动第二次会报错java.lang.IllegalThreadStateException
- 剑指OFFER:二维数组中的查找
- Linux页表初始化
- Java操作Excel导出数据到Excel文档
- LeetCode总结