算法导论--装备线调度(升序&&降序输出)
题意就先不用讲了吧,感觉自己还没有掌握核心的东西。
//心得
//如何保持路径,递归的实现 #include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
using namespace std;
int a[100][100];//time for station
int t[100][100];//time for from Li to Lj
int f[100][100];//recorded time
int l[100][100];//keep trace of fastest ways,比方l[1][6]==2,说明l[1][6]是从
//lines 2过来的
int e[2];//into time
int ss[100];
int x[2];//depart time
int last_time; /*******test data*********
1
6
7 9 3 4 8 4
8 5 6 4 5 7
2 3 1 3 4
2 1 2 2 1
2 4
3 2
*************************/
/********core code******/ int assmbly_line(int m)
{
int ff;
f[1][1]=e[1]+a[1][1];
f[2][1]=e[2]+a[2][1];//開始时比較进入哪一条装配线
// printf("\n%d %d",f[1][1],f[2][1]);
for(int j=2;j<=m;j++){//最优化选择
if(f[1][j-1]+a[1][j]<=f[2][j-1]+a[1][j]+t[2][j-1])
{
f[1][j]=f[1][j-1]+a[1][j];
l[1][j]=1;
// printf("j=%d 1\t",l[1][j]);
}
else
{
f[1][j]=f[2][j-1]+a[1][j]+t[2][j-1];
l[1][j]=2;
// printf("j=%d 2\t",l[2][j]);
}
if(f[2][j-1]+a[2][j]<=f[1][j-1]+t[1][j-1]+a[2][j])
{
f[2][j]=f[2][j-1]+a[2][j];
l[2][j]=2;
// printf("j=%d 2\t",l[2][j]);
}
else
{
f[2][j]=f[1][j-1]+t[1][j-1]+a[2][j];
l[2][j]=1;
// printf("j=%d 1\t",l[2][j]);
}
} if(f[1][m]+x[1]<=f[2][m]+x[2])
{
int i=1; ff=f[1][m]+x[1]; last_time=1;
}
else
{ ff=f[2][m]+x[2];
last_time=2; }
return ff; }
//打印路径降序
void print_lines(int s[100],int ll,int m)
{
int i=ll;
ss[m]=i;//为了后面的升序使用
printf("lines: %d,stations: %d\n",i,m);
for(int j=m;j>=2;j--)
{
i=l[i][j];
printf("lines: %d,stations: %d\n",i,j-1);
ss[j-1]=i;
}
}
//打印路径升序递归
void print_cursion_line( int l[][100],int last_l,int n)
{
int i=last_l;
if(n==1)
printf("Lines %d,stations %d\n",i,n);
else
{
print_cursion_line(l,l[i][n],n-1);
printf("Lines %d,stations %d\n",i,n);
} }
//打印路径非递归
void nocursion_increasing(int ss[100],int m)
{
for(int i=1;i<=m;i++)
printf("lines %d,stations %d\n",ss[i],i);
} //*******core code******//
int main()
{
int m;
freopen("in.txt","r",stdin);
int k;
scanf("%d",&k);//測试的数据块数
while(k--){
memset(a,0,sizeof(a));
memset(t,0,sizeof(t));
memset(f,0,sizeof(f));
memset(l,0,sizeof(l));
memset(e,0,sizeof(e));
memset(x,0,sizeof(x));
scanf("%d",&m);//装配站的数目
for(int i=1;i<=m;i++)
scanf("%d",&a[1][i]);
for(int i=1;i<=m;i++)
scanf("%d",&a[2][i]);
for(int i=1;i<m;i++)
scanf("%d",&t[1][i]);
for(int i=1;i<m;i++)
scanf("%d",&t[2][i]);
for(int i=1;i<=2;i++)
scanf("%d",&e[i]);
for(int i=1;i<=2;i++)
scanf("%d",&x[i]); } printf("\n");
int cai=assmbly_line(m);
printf("The total cast time is %d\n",cai); int ll=last_time;
//下面是打印的路径
printf("decreaing output is \n");
print_lines(l[m],ll,m);
printf("Increaing and oncursion output is \n");
nocursion_increasing(ss,m);
printf("increaing cursion is \n");
print_cursion_line(l,ll,m);
// printf("last_time=%d\n",last_time);
}
算法导论--装备线调度(升序&&降序输出)的更多相关文章
- 奇数结点升序偶数结点降序的单链表排序(Python实现)
题目 一个链表,奇数结点升序,偶数结点降序,要求变成一个全升序的链表. 例如:1->8->2->7->3->6->4->5,变为1->2->3-& ...
- DataTable进行排序Asc升序,Desc降序
DataTable dt = new DataTable(); DataView dv = dt.DefaultView; dv.Sort = "XXX Asc"; dt=dv.T ...
- mysql中的升序和降序以及一个字段升序和一个字段降序
mySql中,升序为asc,降序为desc.例如: 升序:select * from 表名 order by 表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select ...
- SQL-ORDER BY 多字段排序(升序、降序)
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */ ORDER BY _column1, _column2 DESC; /* _col ...
- 升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)
package Day28ketangzuoye; import java.util.ArrayList; import java.util.Collections; import java.util ...
- mysql 升序降序
默认不指定,order by 按照升序排列. asc:升序 desc:降序
- mysql中一个字段升序,另一个字段降序
mySql中,升序为asc,降序为desc.例如: 升序:select * from 表名 order by 表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select ...
- sql中使一个字段升序,一个字段降序
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */ ORDER BY _column1, _column2 DESC; /* _colum ...
- LINQ中的OrderBy实现按照两个字段升序、降序排序操作
在公司或许有这种需求,先根据第一个某个字段按照升序排序,然后如果相同,在按照第二个某个字降序排序,我们该怎么去实现呢? 现在来教教大家分别使用Labmda和LINQ进行这种操作. 1.先按照第一个字段 ...
随机推荐
- ArcGIS Pro体验04——菜单栏
对菜单栏进行熟悉一下: 1.地图菜单 剪切板(Clipboard):剪切(Cut).复制(Copy).粘贴(Paste),这些不用说了,在ArcMap中是放在"编辑"菜单下面的.当 ...
- Python面向对象编程 - 一个记事本程序范例(二)
给程序加上控制台菜单 menu.py import sys from notebook import Notebook, Note class Menu: '''Display a menu and ...
- MVC应用积累
1.Controller中的跳转 (1)直接Redirect后加(Controller/Action):Response.Redirect("/Home/Index"); (2)直 ...
- Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xml, reason: Connection to https://dl-ssl.google.com refused
修改hosts文件.文件在C:\WINDOWS\system32\drivers\etc目录下,Linux用户打开/etc/hosts文件.用记事本打开文件后添加以下内容. #Google主页203. ...
- 《Java并发编程实战》第九章 图形用户界面应用程序界面 读书笔记
一.为什么GUI是单线程化 传统的GUI应用程序通常都是单线程的. 1. 在代码的各个位置都须要调用poll方法来获得输入事件(这样的方式将给代码带来极大的混乱) 2. 通过一个"主事件循环 ...
- 通过jaxws-ri创建webservice服务端和客户端
1. 获得开发包 当然是到 SUN 的开发网站下载 JAX-WS RI,或者下载我的网盘备份 ,下载下来的只是一个jar包,参考官网上的方法在命令行调用:java -jar JAXWS2.1.2-20 ...
- 一致性 hash 算法( consistent hashing )(转)
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache系统中应用越来越广泛: 1 基 ...
- urlparse模块(专门用来解析URL格式)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #urlparse模块(专门用来解析URL格式) #URL格式: #protocol ://hostname[ ...
- 修改wamp apache 默认端口
目标:新配置一个虚拟主机,要求端口为8080,但要保证原来的80端口仍可用 1.修改 httpd.conf 开启vhost文件支持 2.httpd-vhosts.conf 中增加,记得把原来的内容全部 ...
- C#:设置焦点在最小的TabIndex控件上
private void FocusFirstTabIndex(Control container) { // init search result varialble Control searchR ...