Problem4-1013
来源:互联网 发布:sql 清空数据库所有表 编辑:程序博客网 时间:2024/04/30 01:30
简单题意:车票的价格决定于两个站点之间的距离。给出了描述距离和价钱之间关系的表格。求花费最少
解题思路:用Floyd算法,借鉴了网上的算法,他们用的是_int64,直接用int不知道有没有问题;
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const __int64 inf = 1e18;
__int64 map[105][105];
__int64 k[105];
__int64 a[5];
__int64 c[5];
__int64 n,m;
__int64 judge(__int64 x)
{
if(x>0 && x<=a[1])
return c[1];
if(x<=a[2])
return c[2];
if(x<=a[3])
return c[3];
if(x<=a[4])
return c[4];
return inf;
}
__int64 kabs(__int64 a)
{
return a<0?-a:a;
}
void floyd()
{
__int64 i,j,k;
for(k = 1; k<=n; k++)
for(i = 1; i<=n; i++)
for(j = 1; j<=n; j++)
if(map[i][j]>map[i][k]+map[k][j])
map[i][j] = map[i][k]+map[k][j];
}
int main()
{
__int64 t,i,j,x,y,cas = 1;
scanf("%I64d",&t);
while(t--)
{
for(i = 1; i<=4; i++)
scanf("%I64d",&a[i]);
for(i = 1; i<=4; i++)
scanf("%I64d",&c[i]);
scanf("%I64d%I64d",&n,&m);
for(i = 1; i<=n; i++)
scanf("%I64d",&k[i]);
for(i = 0; i<=n; i++)
{
for(j = 0; j<=n; j++)
map[i][j] = inf;
map[i][i] = 0;
}
for(i = 1; i<=n; i++)
for(j = i; j<=n; j++)
{
map[i][j] = map[j][i] = judge(kabs(k[j]-k[i]));
}
floyd();
printf("Case %I64d:\n",cas++);
while(m--)
{
scanf("%I64d%I64d",&x,&y);
if(map[x][y] == inf)
printf("Station %I64d and station %I64d are not attainable.\n",x,y);
else
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",x,y,map[x][y]);
}
}
return 0;
}
#include <string.h>
#include <algorithm>
using namespace std;
const __int64 inf = 1e18;
__int64 map[105][105];
__int64 k[105];
__int64 a[5];
__int64 c[5];
__int64 n,m;
__int64 judge(__int64 x)
{
if(x>0 && x<=a[1])
return c[1];
if(x<=a[2])
return c[2];
if(x<=a[3])
return c[3];
if(x<=a[4])
return c[4];
return inf;
}
__int64 kabs(__int64 a)
{
return a<0?-a:a;
}
void floyd()
{
__int64 i,j,k;
for(k = 1; k<=n; k++)
for(i = 1; i<=n; i++)
for(j = 1; j<=n; j++)
if(map[i][j]>map[i][k]+map[k][j])
map[i][j] = map[i][k]+map[k][j];
}
int main()
{
__int64 t,i,j,x,y,cas = 1;
scanf("%I64d",&t);
while(t--)
{
for(i = 1; i<=4; i++)
scanf("%I64d",&a[i]);
for(i = 1; i<=4; i++)
scanf("%I64d",&c[i]);
scanf("%I64d%I64d",&n,&m);
for(i = 1; i<=n; i++)
scanf("%I64d",&k[i]);
for(i = 0; i<=n; i++)
{
for(j = 0; j<=n; j++)
map[i][j] = inf;
map[i][i] = 0;
}
for(i = 1; i<=n; i++)
for(j = i; j<=n; j++)
{
map[i][j] = map[j][i] = judge(kabs(k[j]-k[i]));
}
floyd();
printf("Case %I64d:\n",cas++);
while(m--)
{
scanf("%I64d%I64d",&x,&y);
if(map[x][y] == inf)
printf("Station %I64d and station %I64d are not attainable.\n",x,y);
else
printf("The minimum cost between station %I64d and station %I64d is %I64d.\n",x,y,map[x][y]);
}
}
return 0;
}
0 0
- Problem4-1013
- Problem4
- Problem4
- snort problem4
- Problem4-1001
- Problem4-1003
- Problem4-1005
- Problem4-1008
- Problem4-1012
- Problem4-1011
- Problem4-1019
- Problem4-1004
- Problem4-1006
- Problem4-1009
- Problem4-总结报告
- Problem4-1018
- 剑指Offer-java problem4
- Unit6--problem4--多文件组织多个类的程序
- ORACLE数据泵导入导出案例(expdp & impdp)
- Qt 开发activex控件(以中控的指纹仪URU4000B为例)
- RecyclerView中的item的match_parent属性失效问题解决方案。
- 一位基地学长(华科特优生)的励志贴: 大学生活点滴回忆录
- 一些好玩的题目
- Problem4-1013
- 读《ECMAScript6 入门》记1-5章
- Recall(召回率) Precision(准确率) F-Measure E值 sensitivity(灵敏性) specificity(特异性)漏诊率 误诊率 ROC AUC
- Selenium 新窗口处理方法
- android 5.0 Launcher TV 开发 1
- RxJava 基础篇
- java.lang.IllegalStateException: failed to create a child event loop
- 线程与进程之间的联系和区别
- oracle恢复大全