写了一天多,刚刚写完,脑壳有点疼。
先上代码,缓一会再写解析。
这个代码能所有结果都正确,但是超时了。不过好在代码的思路直观,并没有太多太复杂的抽象算法。虽然只有七十分,但是我觉得还是可以分享一下的。
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;//字符串分词器
import java.io.InputStreamReader;//输入流读取
import java.io.IOException;//输入流错误抓取
import java.io.BufferedReader;//缓存读取
class Reader{//io流输入
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer token1 =new StringTokenizer("");
static StringTokenizer token2 =new StringTokenizer("");
static String next() throws IOException{
while(!token1.hasMoreElements()){
token1=new StringTokenizer(read.readLine());
}
return token1.nextToken();
}
static int nextInt() throws IOException{
return Integer.parseInt(next());
}
static ArrayList<Integer> List() throws IOException{//输入2个或者3个数据,存入数组并返回
ArrayList<Integer> temp=new ArrayList<Integer>();
token2 =new StringTokenizer(read.readLine());
while(token2.hasMoreTokens())
temp.add(Integer.parseInt(token2.nextToken()));
return temp;
}
}
class Node{//单个结点
private boolean state;//状态量,数据更新之后改为true,数据发送之后改为false。
public ArrayList<Integer> blockchain = new ArrayList<Integer>();
public Node() {//常规初始化
blockchain.add(0);
this.state=false;
}
public Node(ArrayList<Integer> blockchain) {//深克隆初始化
this.blockchain=blockchain;
}
public Node Deepclone() {//深克隆
ArrayList<Integer> temp=new ArrayList<Integer>();
for(int i:blockchain) {
temp.add(i);
}
return new Node(temp);//返回一个全新的深克隆对象
}
public void add_block(int x) {//添加区块
blockchain.add(x);
this.state=true;//添加新区块,数据被更新,状态量改为true。
}
private void contrast(Node temp) {
this.blockchain.clear();
for(int i:temp.blockchain) {
this.blockchain.add(i);
}
this.state=true;//主链被更换,数据更新,状态量改为true。
}
public void Reset(Node temp) {
if(temp.blockchain.size()>this.blockchain.size()) {
this.contrast(temp);
}
else if((temp.blockchain.size()==this.blockchain.size())&&(temp.blockchain.get(temp.blockchain.size()-1)<this.blockchain.get(this.blockchain.size()-1))) {
this.contrast(temp);
}
}
public void show(){
System.out.print(this.blockchain.size()+" ");
for(int i:this.blockchain) {
System.out.print(i+" ");
}
System.out.print("\n");
}
public void befalse() {//某时刻检测到改点状态量为true,则将该点的状态信息克隆打包后送到队列中去。
this.state=false;
}
public boolean ReturnState() {
return this.state;
}
}
class DataPacket{//打包一个发送数据包,发进一个队列中
public int from;//发出者
public int to;//接收者
public int t;//发出时间
public Node node;//将某个结点的某个状态深克隆后保存下来,形成一个静态的状态结点。注意必须进行深克隆,否则不能保证结点保存一个静态状态。
public DataPacket(int from,int to,int t,Node node) {//node对象必须是原对象的深克隆,不能直接把原对象的索引放进来。
this.from=from;
this.to=to;
this.t=t;
this.node=node;
}
}
class NodeList{
private int n;//结点个数
private int t_old;//上一次时间递推的终点。
private int t_new;//这一次时间递推的起点。
private int t;//时延
private Node [] nodelist;//结点信息表,数组下标就是该结点的序号
private int [][] route;//节点间的通路矩阵
private Queue<DataPacket> queue =new LinkedList<DataPacket>();
public NodeList(Node [] nodelist,int [][] route,int n,int t) {
this.n=n;
this.t_old=0;
this.t_new=0;
this.nodelist=nodelist;
this.route=route;
this.t=t;
}
private void advance() {//做时间线推进。先检测发送端是否有需要发送的结点信息,再检验接收端是否有需要接受的结点信息。
for(int i=this.t_old;i<=this.t_new;i++) {//时间线推进
//时间线推进过程中,必须先处理接收端,再处理发送端
//如果先处理发送端,再处理接收端的话。接收端接到的信息不能立刻进行转发,而需要等到下一个时刻再转发,导致结果错误。
//i时刻下,接收端事件处理。
while(queue.peek()!=null&&(i-queue.peek().t)>=t) {//判断列表中的包是否到达。
DataPacket temp=queue.poll();//数据包出队列
nodelist[temp.to].Reset(temp.node);//更新操作
}
//i时刻下,发送端事件处理。
for(int j=1;j<=n;j++) {//结点遍历
if(nodelist[j].ReturnState()==true) {//判断这个结点的信息是否有被跟新过
for(int k=1;k<=n;k++) {
if(route[j][k]==1) {//遍历路径矩阵,找到与这个点相通的点
queue.offer(new DataPacket(j,k,i,nodelist[j].Deepclone()));//数据包入队列
}
}
nodelist[j].befalse();
}
}
}
}
public Node query(int a,int b) {
t_new=b;//进入操作,更新时间
this.advance();//推进时间线
t_old=t_new;//操作完成,保存时间
return nodelist[a].Deepclone();//返回一个深克隆对象用于保存某个状态
}
public void add(int a,int b,int c) {
t_new=b;
this.advance();
nodelist[a].add_block(c);//时间线推进结束之后,加入新的区块。
t_old=t_new;
}
}
public class Main {
public static void main(String[] args) throws IOException{
int n=Reader.nextInt();//点的数目
int m=Reader.nextInt();//边的数目
Node [] nodelist=new Node[n+1];
int [][] route=new int[n+1][n+1];
for(int i=1;i<=n;i++) //结点从1开始,结点数组也从1开始
nodelist[i]=new Node();//结点初始化
for(int i=0;i<m;i++) {//创建通路矩阵
int j=Reader.nextInt();
int k=Reader.nextInt();
route[j][k]=1;//矩阵横纵都从1开始
route[k][j]=1;
}
int t=Reader.nextInt();//传输延时
NodeList list=new NodeList(nodelist,route,n,t);
int k=Reader.nextInt();//操作次数
Node [] output=new Node[k];//保存查询结果,其中元素都是深克隆形成的静态的状态值。
int z=0;//查询次数
for(int i=0;i<k;i++) {
ArrayList<Integer> temp=Reader.List();
if(temp.size()==2) {//双参查询处理
output[z]=list.query(temp.get(0),temp.get(1));
z+=1;
}
if(temp.size()==3) {//三参添加处理
list.add(temp.get(0), temp.get(1), temp.get(2));
}
}
for(int i=0;i<z;i++) {
output[i].show();
}
}
}