usaco butter java

来源:互联网 发布:c语言循环左移 编辑:程序博客网 时间:2024/05/17 00:52
/*ID: daniel.20LANG: JAVATASK: butter*/import java.util.*;import java.io.*;class node{    LinkedList<Integer> list;    LinkedList<Integer> dis;    node(){        list = new LinkedList<Integer>();        dis = new LinkedList<Integer>();    }    public void add(int a,int b){        this.list.add(a);        this.dis.add(b);    }}public class butter {    static int farm,path,cows;    static int cowLocation[];    static node nodes[];    static int dist[][];    static void spfa(int source){        LinkedList<Integer> queue = new LinkedList<Integer>();        queue.add(source);        boolean marked[] = new boolean[farm+1];        Arrays.fill(dist[source], 99999999);        Arrays.fill(marked, false);        dist[source][source] = 0;        marked[source] = true;        while(!queue.isEmpty()){            int u = queue.pop();            LinkedList<Integer> edge = nodes[u].list;            for(int i=0;i<edge.size();i++){                int v = edge.get(i);                int w = nodes[u].dis.get(i);                if(dist[source][u]+w<dist[source][v]){                    dist[source][v] = w+dist[source][u];                    if(!marked[v]){                        marked[v] = true;                        queue.add(v);                    }                }            }            marked[u] = false;        }    }    static void work() throws IOException{        int min = Integer.MAX_VALUE;        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("butter.out")));        dist = new int[farm+1][farm+1];        for(int i=1;i<farm+1;i++){            spfa(i);        }        for(int i=1;i<farm+1;i++){            int sum = 0;            for(int j=0;j<cows;j++){                int location = cowLocation[j];                sum += dist[i][location];            }            min = min<sum?min:sum;        }        System.out.println(min);        out.println(min);        out.close();    }    public static void main(String[] args) throws Exception {        Scanner cin = new Scanner(new File("butter.in"));        //Scanner cin = new Scanner(System.in);        cows = cin.nextInt();        farm = cin.nextInt();        path = cin.nextInt();        cowLocation = new int[cows];        for(int i=0;i<cows;i++){            cowLocation[i] = cin.nextInt();        }        nodes = new node[farm+1];        for(int i=0;i<farm+1;i++) nodes[i] = new node();        for(int i=0;i<path;i++){            int from = cin.nextInt();            int to = cin.nextInt();            int w = cin.nextInt();            nodes[from].add(to, w);            nodes[to].add(from,w);        }        work();    }}

   Test 1: TEST OK [0.144 secs, 274800 KB]   Test 2: TEST OK [0.115 secs, 274800 KB]   Test 3: TEST OK [0.173 secs, 274800 KB]   Test 4: TEST OK [0.202 secs, 274800 KB]   Test 5: TEST OK [0.288 secs, 275824 KB]   Test 6: TEST OK [0.360 secs, 275824 KB]   Test 7: TEST OK [0.526 secs, 275824 KB]   Test 8: TEST OK [0.670 secs, 275824 KB]   Test 9: TEST OK [0.878 secs, 274800 KB]   Test 10: TEST OK [0.799 secs, 275824 KB]
题意是很明显的,最短路径. 而且是如此明显的所有点对所有点最短路径,第一反映就是floyd.

第二反应就是要超时...看了一下数据量800个点 接近10^9了,还是写了一下看,其实floyd大概也就是1.1秒就能过.

其实数据就是在卡floyd. 然后换SPFA上吧,优化过的dijkstra也能过.

很久没写图论, SPFA都不太会写了... 奇怪的是邻接矩阵的spfa居然过不去,也被卡住了,时间和floyd差不多,

邻接矩阵不应该这么慢啊,不是所有点都被松弛,用队列应该能优化时间的...不是很明白.

当然,所有点都遍历去找边是会慢,800个点呢. 但是居然和floyd一样了...

最后还是邻接表SPFA

Java随便写一点就很长啊,又是100行的代码. 不知道哪里有写的简短的java代码看,哎...