一、题目:
问题描述
小明和小芳出去乡村玩,小明负责开车,小芳来导航。
小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公里小明会增加s2的疲劳度。
例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2)2+2+22=16+2+4=22。
现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。
输入格式
输入的第一行包含两个整数n, m,分别表示路口的数量和道路的数量。路口由1至n编号,小明需要开车从1号路口到n号路口。
接下来m行描述道路,每行包含四个整数t, a, b, c,表示一条类型为t,连接a与b两个路口,长度为c公里的双向道路。其中t为0表示大道,t为1表示小道。保证1号路口和n号路口是连通的。
输出格式
输出一个整数,表示最优路线下小明的疲劳度。
样例输入
6 7
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
样例输出
76
样例说明
从1走小道到2,再走小道到3,疲劳度为52=25;然后从3走大道经过4到达5,疲劳度为20+30=50;最后从5走小道到6,疲劳度为1。总共为76。
数据规模和约定
对于30%的评测用例,1 ≤ n ≤ 8,1 ≤ m ≤ 10;
对于另外20%的评测用例,不存在小道;
对于另外20%的评测用例,所有的小道不相交;
对于所有评测用例,1 ≤ n ≤ 500,1 ≤ m ≤ 105,1 ≤ a, b ≤ n,t是0或1,c ≤ 105。保证答案不超过106。
二、我的思路
用dijkstra算法堆优化版,加上一点改动,对于存入优先队列的结点,记录其上一条道路的类型和长度,在比较疲劳度的时候,根据上条路的情况和本条路的情况对比,选择疲劳度更小的路。
最开始不知道为什么只有20分,后来发现自己在输入时,把每条道路当做有向边输入了,改成双向的就变成100分。
但是过了一会再测试,就一直是90分,应该是最后一个测试数据在超时的边缘徘徊把…
三、代码 (90分)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
class Node {
int type;
int p;
int w;
int before;
int bdis;
public Node(int type,int p,int w) //构造邻接表时使用
{
this.type=type;
this.p=p;
this.w=w;
}
public Node(int p,int w,int before,int bdis) //添加优先队列节点时使用
{
this.p=p;
this.w=w;
this.before=before; //上条路的类型,0没有上条路,1表示上一条路为大路,2表示上一条路为小路
this.bdis=bdis; //上条路的长度(大小路交替时更新为0)
}
}
class NodeComparator implements Comparator<Node> {
@Override
public int compare(Node o1, Node o2) {
// TODO Auto-generated method stub
return o1.w-o2.w<0?-1:1;
}
}
public class Main {
static int n,m;
static List<Node>[] g; //邻接表
static boolean[] vis; //访问标记
static int[] dist; //源点到各个点所需的最小疲劳度
static Queue<Node> sup = new PriorityQueue<Node>(new NodeComparator());
//优先队列+自定义比较器
static int INF = Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
g = new List[n];
for(int i=0;i<n;i++)
g[i]=new ArrayList<Node>();
for(int i=0;i<m;i++)
{
int type = in.nextInt();
int from = in.nextInt();
int to = in.nextInt();
int weight = in.nextInt();
g[from-1].add(new Node(type,to-1,weight));
g[to-1].add(new Node(type,from-1,weight)); //加上这一句从20分到了100分
}
dijkstra();
System.out.print(dist[n-1]);
}
private static void dijkstra()
{
dist = new int[n];
vis = new boolean[n];
Arrays.fill(vis, false);
Arrays.fill(dist, INF);
dist[0]=0;
sup.add(new Node(0,0,0,0));
while(!sup.isEmpty())
{
Node pos = sup.poll();
int ppos = pos.p;
if(vis[ppos]) continue;
vis[ppos]=true;
for(int i=0;i<g[ppos].size();i++)
{
Node to = g[ppos].get(i);
if(!vis[to.p])
{
if((pos.before==0||pos.before!=to.type+1)&&dist[ppos]+Math.pow(to.w,to.type+1)<dist[to.p])
{ 上条路不存在或上条路和这条路类型不同 疲劳值不需累加
dist[to.p]=(int) (dist[ppos]+Math.pow(to.w,to.type+1));
sup.add(new Node(to.p,dist[to.p],to.type+1,to.w));
}
else if(pos.before==to.type+1&&dist[ppos]-Math.pow(pos.bdis, to.type+1)+Math.pow(to.w+pos.bdis, to.type+1)<dist[to.p])
{ //上条路和这条路类型相同 疲劳值需要累加
dist[to.p]=(int) (dist[ppos]-Math.pow(pos.bdis, to.type+1)+Math.pow(to.w+pos.bdis, to.type+1));
sup.add(new Node(to.p,dist[to.p],to.type+1,pos.bdis+to.w));
}
}
}
}
}
}