//Sun's NIO2 channel implementation class private WindowsAsynchronousSocketChannelImpl channel;
//nio2 framework core data structure
PendingIoCache ioCache;
//some field retrieve from sun channel implementation class private Object writeLock; private Field writingF; private Field writeShutdownF; private Field writeKilledF; // f
WindowsTransmitFileSupport()
{ //dummy one for JNI code }
/**
* */ public WindowsTransmitFileSupport(
AsynchronousSocketChannel
channel) {
this.channel = (WindowsAsynchronousSocketChannelImpl)channel; try {
// Initialize the fields
Field f = WindowsAsynchronousSocketChannelImpl.class
.getDeclaredField("ioCache");
f.setAccessible(true);
ioCache = (PendingIoCache) f.get(channel);
f = AsynchronousSocketChannelImpl.class
.getDeclaredField("writeLock");
f.setAccessible(true);
writeLock = f.get(channel);
writingF = AsynchronousSocketChannelImpl.class
.getDeclaredField("writing");
writingF.setAccessible(true);
/**
* Implements the task to initiate a write and the handler to consume the
* result when the send file completes. */ privateclass SendFileTask<V, A>implements Runnable, Iocp.ResultHandler { privatefinal PendingFuture<V, A> result; privatefinallong file;//file is windows file HANDLE
// get an OVERLAPPED structure (from the cache or allocate) overlapped = ioCache.add(result); int n = transmitFile0(channel.handle, file, overlapped); if (n == IOStatus.UNAVAILABLE) { // I/O is pending pending =true; return;
} if (n == IOStatus.EOF) { // special case for shutdown output shutdown =true; thrownew ClosedChannelException();
} // write completed immediately thrownew InternalError("Write completed immediately");
} catch (Throwable x) { // write failed. Enable writing before releasing waiters. channel.enableWriting(); if (!shutdown && (x instanceof ClosedChannelException))
x =new AsynchronousCloseException(); if (!(x instanceof IOException))
x =new IOException(x);
result.setFailure(x);
} finally { // release resources if I/O not pending if (!pending) { if (overlapped !=0L)
ioCache.remove(overlapped);
public<V extends Number, A> Future<V> sendFile(long file, A att,
CompletionHandler<V, ?super A> handler) {
boolean closed =false; if (channel.isOpen()) { if (channel.remoteAddress ==null) thrownew NotYetConnectedException();
// check and update state synchronized (writeLock) { try{ if (writeKilledF.getBoolean(channel)) thrownew IllegalStateException( "Writing not allowed due to timeout or cancellation"); if (writingF.getBoolean(channel)) thrownew WritePendingException(); if (writeShutdownF.getBoolean(channel)) {
closed =true;
} else {
writingF.setBoolean(channel, true);
}
}catch(Exception e)
{
IllegalStateException ise=new IllegalStateException(" catch exception when write");
ise.initCause(e); throw ise;
}
}
} else {
closed =true;
}
// channel is closed or shutdown for write if (closed) {
Throwable e =new ClosedChannelException(); if (handler ==null) return CompletedFuture.withFailure(e);
Invoker.invoke(channel, handler, att, null, e); returnnull;
}
return implSendFile(file,att,handler);
}
<V extends Number, A> Future<V> implSendFile(long file, A attachment,
CompletionHandler<V, ?super A> handler) { // setup task PendingFuture<V, A> result =new PendingFuture<V, A>(channel, handler,
attachment);
SendFileTask<V,A> sendTask=new SendFileTask<V,A>(file,result);
result.setContext(sendTask); // initiate I/O (can only be done from thread in thread pool) // initiate I/O if (Iocp.supportsThreadAgnosticIo()) {
sendTask.run();
} else {
Invoker.invokeOnThreadInThreadPool(channel, sendTask);
} return result;
}
privatenativeint transmitFile0(long handle, long file, long overlapped);
After
a long lasting war on words, a war on arms finally breaks out between
littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by
KnuthOcean’s force has rendered a total failure of littleken’s command
network. A provisional network must be built immediately. littleken
orders snoopy to take charge of the project.
With the situation
studied to every detail, snoopy believes that the most urgent point is
to enable littenken’s commands to reach every disconnected node in the
destroyed network and decides on a plan to build a unidirectional
communication network. The nodes are distributed on a plane. If
littleken’s commands are to be able to be delivered directly from a
node A to another node B, a wire will have to be built along the
straight line segment connecting the two nodes. Since it’s in wartime,
not between all pairs of nodes can wires be built. snoopy wants the
plan to require the shortest total length of wires so that the
construction can be done very soon.
Input
The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j
for unidirectional command delivery from the former to the latter.
littleken’s headquarter is always located at node 1. Process to end of
file.
Output
For
each test case, output exactly one line containing the shortest total
length of wires to two digits past the decimal point. In the cases that
such a network does not exist, just output ‘poor snoopy’.
Point vertexes[MAX_V]={0}; int parents[MAX_V]={0}; int ranks[MAX_V]={0}; double G[MAX_V][MAX_V]={0}; bool visited[MAX_V]={0}; bool deleted[MAX_V]={0}; int prev[MAX_V]={0};
int nVertex=0; int nEdge=0;
int u_find(int a)
{ if(parents[a]==a)return a;
parents[a]=u_find(parents[a]); return parents[a];
} void u_union(int a,int b)
{ int pa=u_find(a); int pb=u_find(b); if(ranks[pa]==ranks[pb])
{
ranks[pa]++;
parents[pb]=pa;
}elseif(ranks[pa]<ranks[pb])
{
parents[pa]=pb;
} else
{
parents[pb]=pa;
}
}
for (int i =1; i < nVertex; i++) { if(!deleted[i])
{
cost+=G[prev[i]-1][i]; //cout<<"from "<<(prev[i]-1)<<" to "<<i<<" weight:"<<G[prev[i]-1][i]<<endl; }
private final int recurFind(int minStep, Step oneStep, int pos, int jump) {
int toS=makeS();
int r=shortestSteps(toS);
if(r<minStep-1)
{
oneStep.jump=jump;
oneStep.offset=pos;
oneStep.jumpTo=toS;
minStep=r+1;
}
return minStep;
}
public void printPath()
{
int s=initS;
int i=1;
while(s!=0)
{
System.out.println("["+(i++)+"] Frog at #"+jumps[s].offset+" jumps #"+jumps[s].jump);
s=jumps[s].jumpTo;
}
}
private final int makeS() {
int r=0;
int p=1;
for(int i=0;i<7;i++)
{
r+=p*states[i];
p*=3;
}
return r;
}
/**
* @param args
*/
public static void main(String[] args) {
FrogJump fj=new FrogJump();
int steps=fj.shortestSteps(fj.initS);