HDU1385 Minimum Transport Cost
2014-03-03 19:59
232 查看
Minimum Transport Cost
题目链接:Clicke Here~
一道好题,可惜晚上没状态,眼睛痛。T_T所以,就没去想要怎么记录最短路的字典序了。直接看了别人的博客。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 999999;
const int N = 100+5;
int n,s,e,tax
,a
,mp
; //记录从i到j的最小字典序
void Floyd()
{
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
mp[i][j] = j;
for(int k = 1;k <= n;++k)
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j){
int dist = a[i][k]+a[k][j]+tax[k];
if(dist < a[i][j]){
a[i][j] = dist;
mp[i][j] = mp[i][k];
}
if(a[i][j] == dist&&mp[i][j]>mp[i][k]) //更新最小字典序
mp[i][j] = mp[i][k];
}
}
int main()
{
while(scanf("%d",&n),n)
{
int x;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
scanf("%d",&x);
a[i][j] = (x==-1?INF:x);
}
}
for(int i = 1;i <= n;++i)
scanf("%d",&tax[i]);
Floyd();
while(scanf("%d%d",&s,&e),(s!=-1&&e!=-1))
{
printf("From %d to %d :\n",s,e);
printf("Path: %d",s);
int t = s;
while(t!=e)
{
printf("-->%d",mp[t][e]);
t = mp[t][e];
}
printf("\n");
printf("Total cost : %d\n\n",a[s][e]);
}
}
return 0;
}
题目链接:Clicke Here~
一道好题,可惜晚上没状态,眼睛痛。T_T所以,就没去想要怎么记录最短路的字典序了。直接看了别人的博客。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 999999;
const int N = 100+5;
int n,s,e,tax
,a
,mp
; //记录从i到j的最小字典序
void Floyd()
{
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
mp[i][j] = j;
for(int k = 1;k <= n;++k)
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j){
int dist = a[i][k]+a[k][j]+tax[k];
if(dist < a[i][j]){
a[i][j] = dist;
mp[i][j] = mp[i][k];
}
if(a[i][j] == dist&&mp[i][j]>mp[i][k]) //更新最小字典序
mp[i][j] = mp[i][k];
}
}
int main()
{
while(scanf("%d",&n),n)
{
int x;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
scanf("%d",&x);
a[i][j] = (x==-1?INF:x);
}
}
for(int i = 1;i <= n;++i)
scanf("%d",&tax[i]);
Floyd();
while(scanf("%d%d",&s,&e),(s!=-1&&e!=-1))
{
printf("From %d to %d :\n",s,e);
printf("Path: %d",s);
int t = s;
while(t!=e)
{
printf("-->%d",mp[t][e]);
t = mp[t][e];
}
printf("\n");
printf("Total cost : %d\n\n",a[s][e]);
}
}
return 0;
}
相关文章推荐
- MD5加密算法--->java使用md5存储密码
- 2014-03-03安装搜狗输入法
- 几种常见的开源协议比较
- Android反射机制实现与原理
- Linux vmstat命令详解
- win8JAVA环境配置
- 关于查找算法-
- 一个运营的自我修养:初级运营需要了解的书籍、网站、工具
- oracle中的commit-备忘
- ToStringBuilder 学习
- ZOJ Monthly, March 2014 (2014省赛练习)
- 求离散数据极值(点)
- rtp
- 13-14寒假作业2
- Reading Liat
- (转)类的sizeof
- Eclipse格式化代码时不换行与自动换行
- Linux中重定向输出的学习
- [转]nosql数据库学习一:安装和使用memcached
- oracle 存储过程和游标的使用