Exemple #1
0
int main() {
	scanf("%d%d%d%d%d%d", &N, &M, &A, &B, &K, &G);
	edge.resize(N + 1);
	arr.resize(G);
	dist.resize(N + 1);
	fill(dist.begin(), dist.end(), INF);
	for (int i = 0; i < G; i++)
		scanf("%d", &arr[i]);
	int a, b, d;
	for (int i = 0; i < M; i++) {
		scanf("%d%d%d", &a, &b, &d);
		edge[a].push_back({ b,d });
		edge[b].push_back({ a,d });
		mat[a][b] = d;
		mat[b][a] = d;
	}
	int sum = 0;
	for (int i = 1; i < G; i++) {
		int start = arr[i - 1];
		int finish = arr[i];
		sum += mat[start][finish];
		gdist[start][finish] = sum;
		gdist[finish][start] = sum;
	}
	Pq pq;
	pq.push({ 0,A });
	dist[A] = 0;
	while (!pq.empty()) {
		int now = pq.top().second;
		int cost = -pq.top().first;
		pq.pop();
		if (dist[now] < cost)
			continue;
		for (auto i : edge[now]) {
			int there = i.first;
			int there_cost = cost + i.second + add(now, there, cost);
			if (dist[there] > there_cost) {
				dist[there] = there_cost;
				pq.push({ -there_cost,there });
			}
		}
	}
	printf("%d\n", dist[B]);
	return 0;
}
Exemple #2
0
int main()
{
	int n,m,i,j,s,t;
	Jq jtm;
	Rw tm;
	Pq<Jq,Jcmp> js;
	Pq<Rw,cmp> ls;
	Pq<Rw,cmp2> jxls;
	while(1)
	{
		js.cls();ls.cls();jxls.cls();
		printf("请输入机器数,然后回车:\n");
		m = scan();
		for(i = 1;i <= m;i ++)
		{
			jtm.pos = i;
			jtm.s = 0;
			js.push(jtm);
		}
		printf("请输入任务数,然后回车:\n");
		n = scan();
		srand(unsigned (time(NULL)));
		//printf("请输入n个任务开始的时间 + 需要必须处理时间\n");
		printf("任务号  到达时间 必须处理时\n");
		for(i = 1;i <= n;i ++)
		{
			//scanf("%d%d",&s,&t);   
			s = rand()%100; t = rand()%100+1; //各个任务的到达时间 + 必须工作时间
			s = 0;
			printf("%d     %d     %d\n",i,s,t);
			tm.si = s; tm.ti = t;tm.pos = i;
			ls.push(tm);
		}
		printf("任务   处理器  到达时间  处理时间\n");
		for(i = 1;i <= n;i ++)
		{
			jtm = js.front();  //某个有空闲的处理器
			js.pop();
			if(jxls.empty())  //就绪队列是空的,那么处理器等待
			{
				tm = ls.front();
				if(tm.si > jtm.s)
				{
					jtm.s = tm.si;
				}		
			}
			while(!ls.empty())  //取出所有到达的事务,存入就绪队列
			{
				tm = ls.front();
				if(tm.si <= jtm.s)
				{
					jxls.push(tm);
					ls.pop();
				}else break;
			}
			//取出最大就绪事务,进行处理
			tm = jxls.front();
			jxls.pop();
			printf("%-9d%-9d%-9d%-9d\n",tm.pos,jtm.pos,jtm.s,jtm.s+tm.ti);
			if(jtm.s < tm.si) jtm.s = tm.si;
			jtm.s += tm.ti;
			js.push(jtm);		
		}
		while(!js.empty())
		{
			jtm = js.front();
			js.pop();
		}
		printf("完成所有任务用时:%d\n",jtm.s);
	}
	return 0;
}