// Ksruskal's main funtion
int TransportSystem(double& r_cost, double& rr_cost, int r, int n)
{		
	//int r_cost = 0, rr_cost = 0;
	int states = 1;
	InitSet(n); // initially all vertices in disjoint set

	while (!edgeList.empty()) {
		pair<double, ii> front = edgeList.top();
		edgeList.pop();

		if (!IsSameSet(front.second.first, front.second.second)) { // if no cycle, in the disjoint set
			//mst_cost += (-front.first);
			if (-front.first <= r)
				r_cost += (-front.first);
			else if (-front.first > r) {
				rr_cost += (-front.first);
				states++;
			}

			UnionSet(front.second.first, front.second.second);
		}
	}
	return states;
}
Exemple #2
0
int main()
{
    int n,x=0,m;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&m);
		q.push(m);
	}
	int ans=0;
	for(int i=1;i<=n-1;i++)
	{ 
		int b,c;
		b=q.top();q.pop();
		c=q.top();q.pop();
		q.push(b+c);
		printf("%d ",b);
		printf("%d ",c);
		printf("%d\n",ans);
		ans+=b+c;
	}
	printf("%d",ans);
	return 0;
}
Exemple #3
0
void sp(vector<int> & dist, adjType & graph, priority_queue< KeyValPair> & heap, set<int> &found, int maxDist) {

	// all nodes whithin maxDist of start are stored in distances along with their distances
	// dist has to be initialized to -1 and sizez of num vertices of the graph

	while (!heap.empty()) {
		KeyValPair top = heap.top();
		heap.pop();
		top.value = top.value * -1;
		if (dist[top.key] != -1) continue;
		found.insert(top.key);
		dist[top.key] = top.value;
		//cout << "marked " << top.key << " " << top.value << endl;

		for (adjList::iterator i = graph[top.key].begin(); i != graph[top.key].end(); i++) {
			int adjNext = i->first;
			int adjLen  = i->second;
			if ((dist[adjNext] == -1) && (top.value + adjLen <= maxDist)) {
				heap.push(KeyValPair(adjNext, -1 *(top.value + adjLen)));
			}
		}
	}
	return;
}
int main()
{
	int a,n,s,t1,t2;
	while(cin>>n)
	{
		if(n==0)break;
		for(int i = 0; i < n; i++)
		{
			cin>>a;
			q.push(a);
		}
		s = 0;
		while(q.size()>1)
		{
			t1 = q.top(); q.pop();
			t2 = q.top(); q.pop();
			s+=t1+t2;
			q.push(t1+t2);
		}
		cout<<s<<endl;
		q.pop();
	}
	return 0;
}
Exemple #5
0
void dijkstra(int s) {
    for(int i = 0; i <= n; i++) d1[i] = d2[i] = inf;
    while(!pq.empty()) pq.pop();
    d1[s] = 0;
    pq.push(PII(0, s));
    while(!pq.empty()) {
        PII cur = pq.top();
        pq.pop();
        w = cur.first;
        v = cur.second;
        if(d2[v] < w) continue;
        for(int i = 0; i < e[v].size(); i++) {
            int dd = w + e[v][i].w;
            if(d1[e[v][i].v] > dd) {
                swap(d1[e[v][i].v], dd);
                pq.push(PII(d1[e[v][i].v], e[v][i].v));
            }
            if(d2[e[v][i].v] > dd && dd > d1[e[v][i].v]) {
                d2[e[v][i].v] = dd;
                pq.push(PII(d2[e[v][i].v], e[v][i].v));
            }
        }
    }
}
/*************************************************************************************************
函数说明:软中断激活
输入参数:swiId: 待激活软中断ID
输出参数:无
返回值  :成功返回0, 其它情况返回-1
**************************************************************************************************/
int SwiActivate(unsigned int swiId)
{
	if (!swiMap.count(swiId))
		return -1;
	swiQueue.push(swiMap[swiId]);

	while (!swiQueue.empty())
	{
		Node tempNode = swiQueue.top();
		if (runningId != tempNode.swiId)
		{
			int oldRunningId = runningId;
			runningId = tempNode.swiId;
			tempNode.proc();
			runningId = oldRunningId;
			swiQueue.pop();
		}
		else
			break;

	}

	return 0;
}
void dijkstra(int s)
{
    while (!q.empty()) q.pop();
    memset(d,0x3f,sizeof d);

    d[s]=0;
    q.push((Heap_node){s,d[s]});

    while (!q.empty())
    {
        Heap_node x=q.top();q.pop();

        if (d[x.s]<x.d) continue;

        for (int e=fir[x.s];~e;e=nex[e])
        {
            if (d[v[e]]>d[x.s]+w[e])
            {
                d[v[e]]=d[x.s]+w[e];
                q.push((Heap_node){v[e],d[v[e]]});
            }
        }
    }
}
int prime(){
    int ans = 0;
    Node t;
    t.v = 1;
    t.w = 0;
    que.push(t);
    int nume = 0;
    while(!que.empty() && nume < m){
        t = que.top();
        que.pop();
        if(vis[t.v]) continue;
        ans += t.w;
        vis[t.v] = true;
        nume++;
        for(int i = head[t.v]; i != -1; i = edge[i].n)
            if(!vis[edge[i].v]){
                Node tt;
                tt.v = edge[i].v;
                tt.w = edge[i].w;
                que.push(tt);
            }
    }
    return ans;
}
Exemple #9
0
long long minCost(vector <string> road, vector <int> altitude)
{
	memset(vis, 0, sizeof vis);
	int n = road.size();
	long long ans = INF;
	//for (int i=0; i<n; ++i) road[i][i] = 'Y';
	for (int j = 0; j < n; ++j) {
		int d = abs(altitude[j] - altitude[0]);
		state s = {0, j, d};
		pq.push(s);
	}
	while (!pq.empty()) {
		state cur = pq.top(); pq.pop();
		int pos = cur.pos;
		int alt = cur.alt;
		long long dist = cur.dist;
		//cout << pos << ' ' << alt << ' ' << dist << endl;
		if (vis[pos][alt]) continue;
		vis[pos][alt] = 1;		

		if (pos == n-1) { ans = dist; break; }
		
		for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j) 
		if (road[pos][i] == 'Y' && altitude[alt] >= altitude[j])
		{
			if (!vis[i][j]) {
				long long d = dist + abs(altitude[i] - altitude[j]);
				state ss = {i, j, d};
				pq.push(ss);
			}
		}
	}
	if (ans >= INF) return -1;
	else return ans;
}
int main() {
    scanf("%d %d", &v, &e);
    for (int i = 0; i < e; i++) {
        scanf("%d %d %d %d", &m, &n, &d, &s);
        roads[m].push_back(make_pair(n, d * 60.0 / s));
        roads[n].push_back(make_pair(m, d * 60.0 / s));
    }

    num[1] = 0;
    dist[1] = 0.0;
    q.push(make_tuple(0, 0, 1));
    for (int i = 1; i <= v; i++) { num[i] = e; dist[i] = 1e16; }
    
    double this_time, new_cost;
    int this_num, idx, new_idx;

    while (!q.empty()) {
        std::tie(this_time, this_num, idx) = q.top(); q.pop();
        this_time *= -1; this_num *= -1;
        for (auto a : roads[idx]) {
            std::tie(new_idx, new_cost) = a;
            double alt = this_time + new_cost;
            if (alt < dist[new_idx]) {
                dist[new_idx] = alt;
                num[new_idx] = this_num + 1;
                q.push(make_tuple(-alt, -num[new_idx], new_idx));
            } else if (alt == dist[new_idx] &&
                    this_num + 1 < num[new_idx]) {
                num[new_idx] = this_num + 1;
                q.push(make_tuple(-alt, -num[new_idx], new_idx));
            }
        }
    }

    printf("%d\n%d\n", num[v], (int)(round(dist[v] / 3)));    
}
Exemple #11
0
int dijkastra()
{   
    int i,j,k,s,pos=-1;
   
   	for(i=1;i<=n;i++)vis[i]=0;
   
    while(!q.empty())
    {
    
    s=q.top().i;
    q.pop();
    
    
    for(i=0;i<v[s].size();i++)
    {
    if(vis[v[s][i].first])continue;
    if(dis[s]+v[s][i].second<dis[v[s][i].first])
    {
                                                dis[v[s][i].first]=dis[s]+v[s][i].second;
                                                q.push(vertex(v[s][i].first,dis[v[s][i].first]));
    }
    }
    vis[s]=1;
    }
    
    j=0;
	for(i=1;i<=n;i++)
	{  
	   if(dis[i]>j){j=dis[i];pos=i;}
	   

    }
	
	
	return j;
}
int main(){
	 node1.date = 4 ;
	 node2.date = 5 ;
	 node3.date = 4 ;
	 node1.num = 5 ;
	 node2.num = 4 ;
	 node3.num = 3 ;
	q.push(node1) ;
	q.push(node2) ;
	q.push(node3) ;
		node[50].num = 100 ;
	q.push(node[50]) ;

	while(!q.empty()){
		Node temp ;
		temp = q.top() ;
		q.pop() ;
		cout << temp.date << " " << temp.num << endl ;
	}
	return 0 ;
	
	
	
}
int bfs(point s, point t){
    s.v = 0;
    is[s.x][s.y] = true;
    if(s.x == t.x && s.y == t.y) return s.v;
    qu.push(s);
    while(!qu.empty()){
        s = qu.top();
        qu.pop();
        if(s.x == t.x && s.y == t.y) return s.v;
        for(int i = 0; i < 4; i++){
            point s1;
            int dx = s.x + dir[i][0];
            int dy = s.y + dir[i][1];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && !is[dx][dy] && ch[dx][dy] != 'S' && ch[dx][dy] != 'R'){
                is[dx][dy] = true;
                if(ch[dx][dy] == 'B') s1.v = s.v + 2;
                else s1.v = s.v + 1;
                s1.x = dx, s1.y = dy;
                qu.push(s1);
            }
        }
    }
    return -1;
}
Exemple #14
0
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
    int piv = 0, piv2 = n+1, piv3 = 0;
    for (int i=0; i<n; i++) {
        if(gondolaSeq[i] <= n){
            piv = gondolaSeq[i] - i - 1+ n;
            piv %= n;
        }
    }
    int newSeq[100005] = {};
    for (int i=0; i<n; i++) {
        newSeq[i] = gondolaSeq[(i + n - piv)%n];
        if(newSeq[i] > n) pq.push(pi(newSeq[i],i+1));
    }
    while (!pq.empty()) {
        pi x = pq.top();
        pq.pop();
        replacementSeq[piv3++] = x.second;
        for (int i=piv2; i<x.first; i++) {
            replacementSeq[piv3++] = i;
        }
        piv2 = x.first + 1;
    }
    return piv3;
}
int main(){
	
	int n, l;
	int lig[1000007];
	int v[1005] = {0};
	scanf("%d %d",&n,&l);

	for(int i = 0; i < l; i++)
		scanf("%d",&lig[i]);

	if(l < n){
		for(int i = 0; i < n; i++)
			printf("%d %d\n",i+1,i < l ? 1 : 0);
	}
	else{ 
		for(int i = 0; i < n; i++){
			pq.push(make_pair(-lig[i],-i));
			v[i]++;
		}

		for(int i = n; i < l; i++){
			pair<int, int> aux;
			aux = pq.top();
			pq.pop();
			pq.push(make_pair(-lig[i]+aux.first,aux.second));
			v[-aux.second]++;
		}

		for(int i = 0; i < n; i++)
			printf("%d %d\n",i+1,v[i]);
	}




}
int main() {
	int m, n, i;
	scanf("%d", &m);
	for (i = 0; i < m; ++i) {
		int x;
		scanf("%d", &x);
		op[i] = x;
		if (x == 1) scanf("%d", l + i);
		else scanf("%d %d", l + i, c + i);
	}
	scanf("%d", &n);
	for (i = 0; i < n; ++i) scanf("%I64d", a + i), --a[i], q.push({a[i], i});

	long long sz = 0;
	for (i = 0; i < m; ++i) {
		if (op[i] == 1) {
			memo[sz] = l[i], ++sz;
		} else {
			v.push_back({sz - 1, l[i]});
			if (sz + 1LL * l[i] * c[i] > a[n - 1]) break;
			sz += 1LL * l[i] * c[i];
		}
	}

	while (true) {
		while (!q.empty() && memo.count(q.top().first)) ans[q.top().second] = memo[q.top().first], q.pop();
		if (q.empty()) break;
		while (q.top().first > v.back().first) {
			long long x = q.top().first;
			i = q.top().second, q.pop();
			x = (x - v.back().first - 1) % v.back().second;
			q.push({x, i});
		}
		v.pop_back();
	}

	for (i = 0; i < n; ++i) {
		if (i) putchar(' ');
		printf("%d", ans[i]);
	}
	puts("");
	return 0;
}
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    scanf("%d",&n);
    int cnt = 0;
    while(n -- ){
        double ans = 0;
        int lval, rval;
        int inpt;
        scanf("%d",&inpt);
        ++ cnt;
        if(cnt == 1){
            ans = inpt;
            lg_q.push(inpt);
        }else{
            if(inpt <= lg_q.top()){
                lg_q.push(inpt);
                if(lg_q.size() > sm_q.size() + 1){
                    inpt = lg_q.top();
                    lg_q.pop();
                    sm_q.push(inpt);
                }
            }else{
                sm_q.push(inpt);
                if(lg_q.size() < sm_q.size()){
                    inpt = sm_q.top();
                    sm_q.pop();
                    lg_q.push(inpt);
                }
            }
            if(lg_q.size() == sm_q.size()){
                lval = lg_q.top();
                rval = sm_q.top();
                ans = ((double)lval + (double)rval)/2.0;
            }else{
                ans = lg_q.top();
            }
        }
        printf("%.1lf\n",ans);
    }
    return 0;
}
void dijkstra()
{
    int u, temp, now_vex, now_cost;
    memset(flag, 0, sizeof(flag));
    //memset(dist, inf, sizeof(dist));
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= n; i++)
        dist[i][0] = dist[i][1] = inf;

    node.vex = s;
    node.fg = 0;
    heap.push(node);
    //printf("s == %d\n", s);
    dist[s][0] = 0;
    cnt[s][0] = 1;

    while(!heap.empty())
    {
        node = heap.top();
        heap.pop();


        u = node.vex;
        temp = node.fg;

        if (flag[u][temp])
            continue;
        flag[u][temp] = 1;

        for (int i = 0; i < map[u].size(); i++)
        {
            point = map[u][i];
            now_vex = point.vex;
            now_cost = point.cost + dist[u][temp];
            if (now_cost < dist[now_vex][0])
            {
                if (dist[now_vex][0] != inf)
                {
                    dist[now_vex][1] = dist[now_vex][0];
                    cnt[now_vex][1] = cnt[now_vex][0];
                    node.vex = now_vex;
                    node.fg = 1;
                    heap.push(node);
                    //printf("cnt[now_vex][1] == %d\n", cnt[now_vex][1]);
                }
                dist[now_vex][0] = now_cost;
                cnt[now_vex][0] = cnt[u][temp];
                node.vex = now_vex;
                node.fg = 0;
                heap.push(node);
                //printf("cnt[now_vex][0] == %d\n", cnt[now_vex][1]);
            }
            else if (now_cost == dist[now_vex][0])
            {
                cnt[now_vex][0] += cnt[u][temp];
                //printf("cnt[now_vex][0] == %d\n", cnt[now_vex][1]);
            }
            else if (now_cost < dist[now_vex][1])
            {
                dist[now_vex][1] = now_cost;
                cnt[now_vex][1] = cnt[u][temp];
                node.vex = now_vex;
                node.fg = 1;
                heap.push(node);
                //printf("cnt[now_vex][1] == %d\n", cnt[now_vex][1]);
            }
            else if (now_cost == dist[now_vex][1])
            {
                cnt[now_vex][1] += cnt[u][temp];
                //printf("cnt[now_vex][1] == %d\n", cnt[now_vex][1]);
            }
        }
    }
}
Exemple #19
0
      Tuple* produceNextTuple(){
        while(!heap.empty() && !heap.top().isFinal()){
           closestPairEntry e = heap.top();
           heap.pop();
           if(e.final1()){
               R_TreeNode<2,TupleId> n2 = getNode2(e);
               for(int i=0;i<n2.EntryCount();i++){
                  if(n2.IsLeaf()){
                     R_TreeLeafEntry<2,TupleId> 
                        e2 = (R_TreeLeafEntry<2,TupleId>&)n2[i];
                     closestPairEntry entry(e,e2,e.getLevel2()+1);
                     if(!prune(entry)){
                        heap.push(entry);
                     }
                  } else {
                     R_TreeInternalEntry<2> e2 = (R_TreeInternalEntry<2>&)n2[i];
                     closestPairEntry entry(e,e2,e.getLevel2()+1);
                     if(!prune(entry)){
                        heap.push(entry);
                     }
                  }
               }
           }  else if(e.final2()){
               R_TreeNode<2,TupleId> n1 = getNode1(e);
               for(int i=0;i<n1.EntryCount();i++){
                  if(n1.IsLeaf()){
                     R_TreeLeafEntry<2,TupleId> 
                          e1 = (R_TreeLeafEntry<2,TupleId>&)n1[i];
                     closestPairEntry entry(e1,e.getLevel1()+1, e);
                     if(!prune(entry)){
                        heap.push(entry);
                     }
                  } else {
                     R_TreeInternalEntry<2> e1 = (R_TreeInternalEntry<2>&)n1[i];
                     closestPairEntry entry(e1,e.getLevel2()+1, e);
                     if(!prune(entry)){
                        heap.push(entry);
                     }
                  }
               }
           } else { // both are not final
               R_TreeNode<2,TupleId> n1 = getNode1(e);
               R_TreeNode<2,TupleId> n2 = getNode2(e);
               insertPair(n1,e.getLevel1()+1,n2,e.getLevel2()+1);
           }


        }
        if(heap.empty()){ // no more entries
           return 0;
        }
        closestPairEntry e = heap.top();
        heap.pop();
        TupleId tid1 = e.getTid1();
        TupleId tid2 = e.getTid2();
        Tuple* t1 = r1->GetTuple(tid1,true);
        Tuple* t2 = r2->GetTuple(tid2,true);
      
        if(t1==0 || t2==0){
           cerr << "TupleId stored in rtree not found" << endl;
           cerr << " computing closest pair canceled" << endl;
           count = k;
           return 0; 
        }
        Tuple* res = new Tuple(tt);
        Concat(t1,t2,res);
        t1->DeleteIfAllowed();
        t2->DeleteIfAllowed();
        count++;
        return res; 

      }
Exemple #20
0
inline void solve(){
	int n, i, j, w, m, q, sn, u, v, u2, v2, id, ti, m1, s1, m2, s2;
	sd(n);
	id = 0;
	sid.clear();
	sid2.clear();
	for(i = 0; i < N; i++){
		adj[i].clear();
	}
	for(i = 1; i <= n; i++){
		sd(sn); sd(w);
		u = id;
		for(j = 1; j <= sn; j++){
			adj[id + 1].PB(MP(id, w));
			sid[MP(i, j)] = id;
			id++;
			sid2[MP(i, j)] = id;
			id++;
		}
		for(j = 1; j < sn; j++){
			sd(ti);
			u = sid[MP(i, j)];
			v = u + 2;
			adj[u].PB(MP(v, ti));
			adj[v].PB(MP(u, ti));
		}
	}
	sd(m);
	while(m--){
		sd(m1); sd(s1); sd(m2); sd(s2); sd(ti);
		u = sid[MP(m1, s1)];
		v = sid[MP(m2, s2)];
		u2 = u + 1;
		v2 = v + 1;
		adj[u].PB(MP(v2, ti));
		adj[u2].PB(MP(v2, ti));
		adj[v].PB(MP(u2, ti));
		adj[v2].PB(MP(u2, ti));
	}
	sd(q);
	while(q--){
		sd(m1); sd(s1); sd(m2); sd(s2);
		for(i = 0; i < id; i++){
			dis[i] = INF;
			vis[i] = false;
		}
		u = sid[MP(m1, s1)] + 1;
		Q.push(MP(0, u));
		dis[u] = 0;
		while(!Q.empty()){
			u = (Q.top()).S;
			Q.pop();
			if(vis[u] == true){
				continue;
			}
			vis[u] = true;
			for(j = adj[u].size() - 1; j >= 0; j--){
				v = adj[u][j].F;
				w = adj[u][j].S;
				if(dis[v] > dis[u] + w){
					dis[v] = dis[u] + w;
					Q.push(MP(-dis[v], v));
				}
			}
		}
		v = sid[MP(m2, s2)];
		v2 = v + 1;
		if(vis[v] == true || vis[v2] == true){
			printf("%d\n", min(dis[v], dis[v2]));
		}
		else{
			printf("-1\n");
		}
	}
}
Exemple #21
0
void Solve()
{
	int i, j;
	ac p, q;
	for (i = 0; i < n; i++)
	{
		p.x = i;
		p.y = 0;
		p.nMax = a[i][0];
		b[i][0] = a[i][0];
		que.push(p);
		p.y = m - 1;
		p.nMax = a[i][m-1];
		b[i][m-1] = a[i][m-1];
		que.push(p);
	}
	for (i = 1; i < m - 1; i++)
	{
		p.x = 0;
		p.y = i;
		p.nMax = a[0][i];
		b[0][i] = a[0][i];
		que.push(p);
		p.x = n - 1;
		p.nMax = a[n-1][i];
		b[n-1][i] = a[n-1][i];
		que.push(p);
	}
	while (!que.empty())
	{
		p = que.top();
		que.pop();
		if (p.nMax > b[p.x][p.y])
		{
			continue;
		}
		for (i = 0; i < 4; i++)
		{
			q.x = p.x + dir[i][0];
			q.y = p.y + dir[i][1];
			if (q.x >= 0 && q.x < n && q.y >= 0 && q.y < m)
			{
				if (b[q.x][q.y] > p.nMax)
				{
					if (p.nMax >= a[q.x][q.y])
					{
						q.nMax = p.nMax;
					}
					else
					{
						q.nMax = a[q.x][q.y];
					}
					b[q.x][q.y] = q.nMax;
					que.push(q);
				}
			}
		}
	}
/*
	printf("\nok!!!\n\n");
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			printf("%3d", b[i][j]);
		}
		printf("\n");
	}
*/
}
Exemple #22
0
int main(){
    string ins="insert",get="getMin",rem="removeMin";
    int n;
    cin>>n;
    int ans=0;
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        if(s[i][0]=='i'){
            scanf("%d",&a[i]);
            ans++;
            q.push(a[i]);
        }
        else if(s[i][0]=='g'){
            scanf("%d",&a[i]);
            while(!q.empty() &&q.top()<a[i]){
                ans++;
                q.pop();
            }
            if(q.empty()){
                ans+=2;
                q.push(a[i]);
            }
            else if(q.top()>a[i]){
                ans+=2;
                q.push(a[i]);
            }
            else {
                ans++;
            }
        }
        else {
            if(q.empty()){
                ans+=2;
            }
            else {
                ans++;
                q.pop();
            }
        }
    }
    while(!q.empty()){
        q.pop();
    }
    printf("%d\n",ans);
    for(int i=0;i<n;i++){
        if(s[i][0]=='i'){
            printf("insert %d\n",a[i]);
            q.push(a[i]);
        }
        else if(s[i][0]=='g'){
            while(!q.empty() &&q.top()<a[i]){
                printf("removeMin\n");
                q.pop();
            }
            if(q.empty()){
                printf("insert %d\n",a[i]);
                printf("getMin %d\n",a[i]);
                q.push(a[i]);
            }
            else if(q.top()>a[i]){
                printf("insert %d\n",a[i]);
                printf("getMin %d\n",a[i]);
                q.push(a[i]);
            }
            else {
                printf("getMin %d\n",a[i]);
            }
        }
        else {
            if(q.empty()){
                printf("insert 1\n");
                printf("removeMin\n");
            }
            else {
                printf("removeMin\n");
                q.pop();
            }
        }
    }
    return 0;
}
void input()
{
    long i;
    int hurt;
    int id;
    int ntest;
    
    scanf("%d",&ntest);
    id = 0;
    while(ntest--)
    {
        scanf("%ld %ld %ld",&ngame,&blue,&green);
        while(!b.empty())
            b.pop();
        while(!g.empty())
            g.pop();    
        for(i=0;i<blue;i++)
        {
            scanf("%d",&hurt);
            b.push(hurt);
        }
        for(i=0;i<green;i++)
        {
            scanf("%d",&hurt);
            g.push(hurt);
        }
        while(!b.empty() && !g.empty())
        {
            blue = 0;
            green = 0;
            i = 0;
            while(i < ngame && !b.empty() && !g.empty())
            {
                hurt = b.top() - g.top();
                b.pop();
                g.pop();
                if(hurt > 0)
                    bhurt[blue++] = hurt;
                if(hurt < 0)
                    ghurt[green++] = -hurt;
                i++;
            }
            for(i=0;i<blue;i++)
                b.push(bhurt[i]);
            for(i=0;i<green;i++)
                g.push(ghurt[i]);
        }
        if(id)
            printf("\n");
        if(b.empty() && g.empty())
            printf("green and blue died\n");
        else if(b.empty())
        {
            printf("blue wins\n");
            while(!g.empty())
            {
                cout << g.top() << endl;
                g.pop();
            }
        }
        else
        {
            printf("green wins\n");
            while(!b.empty())
            {
                cout << b.top() << endl;
                b.pop();
            }
        }
        id++;
    }
}
Exemple #24
0
int main() {
	int M;
	scanf("%d%d%d%d",&N,&M,&S,&D);
	memset(mat,-1,sizeof(mat));
	for(int i=0;i<M;i++){
		int from, to, dist, cost;
		scanf("%d%d%d%d",&from,&to,&dist,&cost);
		if(mat[from][to][0]==-1 || mat[from][to][0] > dist
				|| (mat[from][to][0]==dist && mat[from][to][1] > cost)){
			mat[from][to][0] = mat[to][from][0] = dist;
			mat[from][to][1] = mat[to][from][1] = cost;
		}
	}

	memset(col,-1,sizeof(col));
	memset(color,0,sizeof(color));
	pre[S]=-1;
	node u ;
	u.to=S;u.dist=0;u.cost = 0;
	pq.push(u);
	while(!pq.empty()){
		while(!pq.empty() && color[pq.top().to]==1) pq.pop();
		if(pq.empty()) break;
		u = pq.top();
		pq.pop();
		col[u.to][0] = u.dist;
		col[u.to][1] = u.cost;
		color[u.to]=1;
		if(u.to == D) break;
		for(int i=0;i<N;i++)
			if(mat[u.to][i][0]!=-1 && color[i]==0){
				if(col[i][0]==-1){
					col[i][0] = u.dist + mat[u.to][i][0];
					col[i][1] = u.cost + mat[u.to][i][1];
					node v;
					v.to = i;
					v.dist = col[i][0];
					v.cost = col[i][1];
					pq.push(v);
					pre[i]=u.to;
				}
				else{
					if(col[i][0] > u.dist+mat[u.to][i][0]
				    || (col[i][0] == u.dist+mat[u.to][i][0] && col[i][1] > u.cost + mat[u.to][i][1])){
						col[i][0] = u.dist + mat[u.to][i][0];
						col[i][1] = u.cost + mat[u.to][i][1];
						node v;
						v.to = i;
						v.dist = col[i][0];
						v.cost = col[i][1];
						pq.push(v);
						pre[i]=u.to;
					}
				}
			}
	}
	if(col[D][0]!=-1){
		int sz=0;
		int cur = D;
		while(cur!=-1){
			res[sz++]=cur;
			cur=pre[cur];
		}
		for(int i=sz-1;i>=0;i--)
			if(i==sz-1)
				printf("%d",res[i]);
			else
				printf(" %d",res[i]);
		printf(" %d %d\n",col[D][0],col[D][1]);
	}
	return 0;
}
void bfs(node s){
	
	int i,j,k;
	node temp,temp1;
    s.rem-=a[1]; s.no=1; s.tot=1;
	ans.tot=inf; ans.DI=inf;
	lect[1][s.rem].DI=0; lect[1][s.rem].tot=1;
	q.push(s);
	
	while(!q.empty()){
		
		//temp=q.front();
		temp=q.top();
		q.pop();
		
		if(temp.no==n){
			if(temp.rem==0) temp.DI=temp.DI;
			else if(temp.rem<=10) temp.DI=temp.DI-c;
			else temp.DI=temp.DI+(temp.rem-10)*(temp.rem-10);
			//printf("%d %d\n",temp.tot,temp.DI);
			if(ans.tot>temp.tot){
				ans=temp;
			}
			else if(ans.tot==temp.tot){
				if(ans.DI>temp.DI) ans=temp;
			}
			continue;
		}
		i=temp.no;
		
		//continue..............
		if(temp.rem>=a[i+1]){
			temp1.rem=temp.rem-a[i+1];
			temp1.no=i+1;
			temp1.DI=temp.DI; temp1.tot=temp.tot;
			if(lect[temp1.no][temp1.rem].tot>temp1.tot){
				lect[temp1.no][temp1.rem]=temp1;
				q.push(temp1);
			}
			else if(lect[temp1.no][temp1.rem].tot==temp1.tot){
				if(lect[temp1.no][temp1.rem].DI>temp1.DI){
					lect[temp1.no][temp1.rem]=temp1;
     				q.push(temp1);
				}
			}
		}
		
		temp1.no=i+1; temp1.tot=temp.tot+1; 
		if(temp.rem==0) temp1.DI=temp.DI;
		else if(temp.rem<=10) temp1.DI=temp.DI-c;
		else temp1.DI=temp.DI+(temp.rem-10)*(temp.rem-10);
		
		temp1.rem=l-a[i+1];

		    if(lect[temp1.no][temp1.rem].tot>temp1.tot){
				lect[temp1.no][temp1.rem]=temp1;
				q.push(temp1);
			}
			else if(lect[temp1.no][temp1.rem].tot==temp1.tot){
				if(lect[temp1.no][temp1.rem].DI>temp1.DI){
					lect[temp1.no][temp1.rem]=temp1;
     				q.push(temp1);
				}
			}
	
		
	//	q.push(temp1);
		
		
	}
	
	
	
}
Exemple #26
0
int main()
{
//	freopen("B.in","r",stdin);
//	freopen("B-small-attempt0.in","r",stdin);freopen("B-small-attempt0.out_2","w",stdout);
//	freopen("B-small-attempt1.in","r",stdin);freopen("B-small-attempt1.out","w",stdout);
	freopen("B-large.in","r",stdin);freopen("B-large.out","w",stdout);
	int testcase;
	scanf("%d",&testcase);
	for (int caseId=1;caseId<=testcase;caseId++)
	{
		for (int i=0;i<60;i++) for (int j=0;j<60;j++) A[i][j]='#';
		scanf("%d%d%d",&n,&m,&limit);
		for (int i=1;i<=n;i++) scanf("%s",A[i]+1);
		while (!M.empty()) M.pop();
		memset(D,255,sizeof(D));
		memset(visited,false,sizeof(visited));
		addnode(1,1,1,0,0);
		int R=-1;
		while (!M.empty())
		{
			int state=M.top().second;
			M.pop();
			int x=state/1000000;
			int y=state/10000%100;
			int s=state/100%100;
			int t=state%100;
			if (visited[x][y][s][t]) continue;
			visited[x][y][s][t]=true;
			int d=D[x][y][s][t];
			if (x==n)
			{
				R=d;
				break;
			}
			for (int dy=-1;dy<=1;dy+=2)
			{
				int y2=y+dy;
				if (!(A[x][y2]=='.' || y2>=s && y2<=t)) continue;
				int x2=x;
				while (A[x2+1][y2]=='.') x2++;
				if (x2-x>limit) continue;
				if (x2==x)
					addnode(x2,y2,s,t,d);
				else
					addnode(x2,y2,1,0,d);
			}
			for (int dy=-1;dy<=1;dy+=2)
			{
				int y3=y-dy;
				if (!(A[x][y3]=='.' || y3>=s && y3<=t)) continue;
				if (A[x+1][y3]=='.') continue;
				for (int y2=y;1;y2+=dy)
				{
					if (!(A[x][y2]=='.' || y2>=s && y2<=t)) break;
					if (A[x+1][y2]=='.') break;
					int y4=y2-dy;
					int x4=x+1;
					while (A[x4+1][y4]=='.') x4++;
					if (x4-x>limit) continue;
					if (x4==x+1)
						addnode(x4,y4,min(y3,y4),max(y3,y4),d+abs(y2-y3));
					else
						addnode(x4,y4,1,0,d+abs(y2-y3));
				}
			}
		}
		printf("Case #%d: ",caseId);
		if (R<0)
			printf("No\n");
		else
			printf("Yes %d\n",R);
		fflush(stdout);
	}
	return 0;
}
void solve() {
    while (!Q.empty()) {
        now = Q.top();
        Q.pop();
        int ii = now.id;
        int r1 = now.rom1;
        int r2 = now.rom2;
        //printf("time: %d\n%d %d %d \n", now.time, ii, r1, r2);
        //if(now.time==36470)
        //    getchar();
        if (r1 == -1) {//表示注册阶段
            tem.rom1=-1;
            tem.time=now.time;
            output[ii].push_back(tem);

            if(input[ii].size() > 0){
                int room = input[ii][0].room;
                if(room/11 > 1){
                    if(tim[0] < now.time){
                        tim[0] = now.time;
                        now.rom1 = 0;
                        now.rom2 = -1;
                        now.id+=100;
                        Q.push(now);
                    }
                    mp[0].insert(make_pair(ii, (room / 11 - 1) * 30));
                }
                else{
                    if(tim[room] < now.time){
                        tim[room] = now.time;
                        now.rom1 = room;
                        now.rom2 = -1;
                        now.id += 100;
                        Q.push(now);
                    }
                    mp[room].insert(make_pair(ii, 1));
                }
            }
            else{
                tem.rom1 = -2;
                tem.time = now.time + 30;
                output[ii].push_back(tem);
            }
        }
        else if (r1 == 0) {//与电梯有关
            if(r2 == 1){
                tem.rom1 = 0;
                tem.rom2 = 1;
                tem.time = now.time;
                output[ii].push_back(tem);

                if(cnt[ii] == input[ii].size()){
                    tem.rom1 = -2;
                    tem.time = now.time + 30;
                    output[ii].push_back(tem);
                }
                else{
                    now.rom2 = input[ii][cnt[ii]].room;
                    now.time += 10;
                    Q.push(now);
                }
            }
            else if(r2 == -1){
                if(mp[0].size()!=0){
                    ii=mp[0].begin()->first;
                    tem.rom1 = 0;
                    tem.rom2 = -1;
                    tem.time = now.time;
                    output[ii].push_back(tem);

                    now.id = ii;
                    now.rom1 = 0;
                    now.rom2 = 1;
                    now.time += mp[0].begin()->second;
                    Q.push(now);
                    mp[0].erase(mp[0].begin());
                    tim[0]+=5;
                    now.id +=100;
                    now.rom2 = -1;
                    now.time = tim[0];
                    Q.push(now);
                }
            }
            else{
                int room = input[ii][cnt[ii]].room;

                tem.rom1 = 0;
                tem.rom2 = room;
                tem.time = now.time;
                output[ii].push_back(tem);

                if(tim[room] < now.time){
                    now.id += 100;
                    tim[room] = now.time;
                    now.rom1 = room;
                    now.rom2 = -1;
                    Q.push(now);
                }
                mp[room].insert(make_pair(ii, 1));
            }
        }
        else {//与房间有关
            if(r2 == 1){
                tem.rom1 = r1;
                tem.rom2 = 1;
                tem.time = now.time;
                output[ii].push_back(tem);

                cnt[ii]++;
                if(cnt[ii] == input[ii].size()){
                    if(r1/11 > 1){
                        now.rom2 = 0;
                        now.time +=10;
                        Q.push(now);
                    }
                    else{
                        tem.rom1 = -2;
                        tem.time = now.time + 30;
                        output[ii].push_back(tem);
                    }
                }
                else{
                    int room = input[ii][cnt[ii]].room;
                    if(room/11 != r1/11){
                        now.rom2 = 0;
                        now.time += 10;
                        Q.push(now);
                    }
                    else{
                        now.rom2 = room;
                        now.time += 10;
                        Q.push(now);
                    }
                }
            }
            else if(r2 == -1){
                if(mp[r1].size() != 0){
                    ii=mp[r1].begin()->first;
                    tem.rom1 = r1;
                    tem.rom2 = -1;
                    tem.time = now.time;
                    output[ii].push_back(tem);

                    now.id = ii;
                    now.rom2 = 1;
                    now.time += input[now.id][cnt[now.id]].time;
                    Q.push(now);
                    now.id += 100;
                    now.rom2 = -1;
                    Q.push(now);
                    tim[r1] = now.time;
                    mp[r1].erase(mp[r1].begin());
                }
            }
            else if(r2 == 0){
                tem.rom1 = r1;
                tem.rom2 = 0;
                tem.time = now.time;
                output[ii].push_back(tem);

                if(tim[0] < now.time){
                    tim[0] = now.time;
                    now.id += 100;
                    now.rom1 = 0;
                    now.rom2 = -1;
                    Q.push(now);
                }
                int xx;
                if(cnt[ii] == input[ii].size()){
                    xx = (r1/11 - 1) * 30;
                }
                else
                    xx = ABS(r1/11 - input[ii][cnt[ii]].room/11) * 30;
                mp[0].insert(make_pair(ii, xx));
            }
            else {
                tem.rom1 = r1;
                tem.rom2 = r2;
                tem.time = now.time;
                output[ii].push_back(tem);

                if(now.time > tim[r2]){
                    tim[r2] = now.time;
                    now.id += 100;
                    now.rom1 = r2;
                    now.rom2 = -1;
                    Q.push(now);
                }
                mp[r2].insert(make_pair(ii, 1));
            }
        }
    }
    return;
}
Exemple #28
0
int main()
{
	int n,flag=1,pos=0;	scanf("%d",&n);
	memset(har,0,sizeof har);
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		int hard;
		scanf("%d",&hard);
		if(flag>=0)
		{
			if(har[hard]==0) 
			{
				har[hard]=i;
				//cout<<"har["<<hard<<"]="<<i<<endl;
			}
			else if(har[hard]>0)
			{
				dou[pos][0]=har[hard];
				dou[pos][1]=i;
				har[hard]=-1;
				//cout<<dou[pos][0]<<":"<<dou[pos][1]<<endl;
				pos++;
				//cout<<"har["<<hard<<"]="<<-1<<endl;
				//cout<<"pos:"<<pos<<endl;
				if(pos>=2) flag=-2;
			}
			else if(har[hard]==-1)
			{
				tri[0]=dou[0][0];
				tri[1]=dou[0][1];
				tri[2]=i;
				flag=-3;
			}
		}
		p_q.push(pii(0-hard,i));
		//cout<<"input:"<<hard<<": "<<i<<" flag:"<<flag<<" pos:"<<pos<<endl;
	}
	if(flag>=0)cout<<"NO"<<endl;
	else 
	{
		int posi=0;
		while(!p_q.empty())
		{
			pii tt=p_q.top();
			ans[posi][0]=ans[posi][1]=ans[posi][2]=tt.second;
			p_q.pop();
			//cout<<ans[posi][0]<<endl;
			posi++;
		}
		if(flag==-2)	
		{
			swap2(dou[0][0]-1,dou[0][1]-1,1);
			swap2(dou[1][0]-1,dou[1][1]-1,2);
		}
		else if(flag==-3)
		{
			swap3(tri[0],tri[1],tri[2]);
		}
		cout<<"YES"<<endl;
		for(int ians=0;ians<3;ians++)
		{
			for(int j=0;j<n-1;j++)
			printf("%d ",ans[j][ians]);
			printf("%d\n",ans[n-1][ians]);
		}
	}
	return 0;
}
Exemple #29
0
int main()
{
    //freopen ("in.txt", "r", stdin);
    int a, b, c;
    scanf ("%d %d %d %d", &v, &n_pumps, &l, &e);
    for (int i = 0; i < e; i++)
    {
        scanf ("%d %d %d", &a, &b, &c);

        graph [a].push_back (edge (b, c));
        graph [b].push_back (edge (a, c));
    }

    end = v;

    for (int i = 0; i < n_pumps; i++)
    {
        scanf ("%d", &a);
        can_refill [a] = true;
    }

    can_refill [1] = true;

    Q.push (for_queue (edge (start, 0), l));

    while (!Q.empty())
    {
        for_queue help = Q.top();
        int current_city = help.E.n;
        int prog_cost = help.E.w;
        int current_fuel = help.fuel;

        Q.pop();

        //printf ("out_while: %d\n", current_city);

        if (!visited [current_city])
        {
            //printf ("current_city: %d\n", current_city);
            visited [current_city] = true;
            if (can_refill [current_city])
            {
                current_fuel = l;
            }
            if (current_city == end)
            {
                printf ("%d\n", prog_cost);
                return 0;
            }

            d [current_city] = prog_cost;

            for (int i = 0; i < graph [current_city].size(); i++)
            {
                int next_city = graph [current_city] [i].n;
                int next_city_cost = graph [current_city] [i].w;
                if (!visited [next_city] && current_fuel >= next_city_cost)
                    Q.push (for_queue (edge (next_city, d [current_city] + next_city_cost), current_fuel - next_city_cost));
            }
        }
    }

    printf ("-1\n");

    return 0;
}
void addNumber(int number, priority_queue<int> &lowers, priority_queue<int,std::vector<int>, std::greater<int> > &highers){
    if(lowers.empty() || number < lowers.top())
        lowers.push(number);
    else
        highers.push(number);
}