当前位置: 首页>>代码示例>>C++>>正文


C++ priority_queue::push方法代码示例

本文整理汇总了C++中priority_queue::push方法的典型用法代码示例。如果您正苦于以下问题:C++ priority_queue::push方法的具体用法?C++ priority_queue::push怎么用?C++ priority_queue::push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在priority_queue的用法示例。


在下文中一共展示了priority_queue::push方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main() {

    // nds
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int id;
        double lat, lon;
        scanf("%d%lf%lf", &id, &lat, &lon);
        st[id] = cnt;
        pos[cnt] = make_pair(lat, lon);
        cnt++;
    }

    // egs
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        x = st[x];
        y = st[y];
        e[x].push_back(y);
        e[y].push_back(x);
    }

    for (int i = 0; i < n; ++i)
        dis[i] = 1e9;


    // find a relative node
    int id = 0;
    for (int i = 0; i < n; ++i)
        if (e[i].size() > 0) {
            id = i;
            break;
        }
    dis[id] = 0;
    Q.push(make_pair(0, id));


    // dij with heap
    for ( ; !Q.empty(); Q.pop()) {

        double cdis = Q.top()._1;
        int cid = Q.top()._2;
        
        if (abs(cdis - dis[cid]) > eps)
            continue;

        for (int i = 0; i < e[cid].size(); ++i) {
            double temp = cdis + cal_dis(pos[cid], pos[e[cid][i]]);
            if (temp < dis[e[cid][i]]) {
                dis[e[cid][i]] = temp;
                Q.push(make_pair(temp, e[cid][i]));
            }
        }
    }

    for (int i = 0; i < n; ++i)
        if (dis[i] < 1e7)
            printf("%d %.5f\n", i, dis[i]);
    

    return 0;
}
开发者ID:voidrank,项目名称:data_structure_data_reformat,代码行数:64,代码来源:mnist.cpp

示例2: sol

void sol(int st, int ed, int n) {
    int v, m = 0, i;
    queue<int> Q;
    Q.push(ed);
    for(i = 1; i <= n; i++)
        used[i] = 0, cdis[i] = dis[i];
    while(!Q.empty()) {
        v = Q.front();
        Q.pop();
        for(it i = g[v].begin();
            i != g[v].end(); i++) {
            if(dis[i->to] == dis[v] - i->w) {
                SEG[m].s = dis[i->to];
                SEG[m].e = dis[v];
                SEG[m].v = v;
                SEG[m++].p = i;
                if(used[i->to] == 0) {
                    used[i->to] = 1;
                    Q.push(i->to);
                }
            }
        }
    }
    sort(SEG, SEG+m, cmps);
    LL ans = 0, odis = dis[ed], tmp;
    it ip;
    int runtime = 0, tj = 0;
    for(i = 0; i < m; i++) {
        while(!pQ2.empty() && SEG[i].s >= pQ2.top().e)
            pQ2.pop();
        tmp = SEG[i].s;
        while(i < m && SEG[i].s == tmp)
            pQ2.push(SEG[i]), i++;
        i--;
        if(pQ2.size() == 1) {
            SEG[tj++] = SEG[i];
            //printf("%lld %lld\n", SEG[i].s, SEG[i].e);
        }
    }
    while(!pQ2.empty()) pQ2.pop();
    sort(SEG, SEG+tj, cmpw);
    for(i = 0; i < tj; i++) {
        //printf("%lld %lld\n", SEG[i].s, SEG[i].e);
        if(SEG[i].p->w <= ans)
            continue;
        //if(runtime > 3000000)   break;
        for(ip = g[SEG[i].p->to].begin();
            ip != g[SEG[i].p->to].end(); ip++) {
            if(ip->to == SEG[i].v)
                break;
        }
        runtime += n;
        for(int j = 1; j <= n; j++) {
            if(cdis[j] <= SEG[i].s)
                pQ.push(ele(j, cdis[j]));
            else
                dis[j] = cdis[j]+ip->w;
            used[j] = 0;
        }
        SEG[i].p->w *= 2;
        ip->w *= 2;
        tmp = dijkstra(st, ed, n);
        if(tmp - odis >= ans)
            ans = tmp - odis;
        if(ans == ip->w/2)
            break;
        ip->w /= 2;
        SEG[i].p->w /= 2;
        i += rand()%2;
    }
    printf("%lld\n", ans);
}
开发者ID:JohnXinhua,项目名称:UVa,代码行数:72,代码来源:a458.cpp

示例3: insert

 void insert(int key, int value) {
     exists[key] = true;
     current[key] = value - delta;
     heap.push(make_pair(-current[key], key));
 }
开发者ID:3013216027,项目名称:acm-icpc,代码行数:5,代码来源:C.cpp

示例4: getSetGo

void getSetGo ()
{
    bool flag=false;
    int i, j;  

    while  (1)
    {
        // Generate Random sequence
		randomize(randomNumberArray,noOfStrings);

        Node temp;
        temp.setOfStringsInMap[randomNumberArray[0]] = setOfStrings.at(randomNumberArray[0]);
        temp.cost=0;

        nodeQueue.push(temp);

        flag = false;

        while (!nodeQueue.empty () && !flag)
        {
            // take out node from queue
            // Check it with all strings 

            for (i = 1; i < setOfStrings.size (); i++)
            {
                // check whether string i is processed in node.
                // If Not then process it with every string present in the node

                Node nodeInProcess = nodeQueue.top ();

                if (nodeInProcess.setOfStringsInMap.size () == noOfStrings)
                {
                    if(nodeInProcess.cost < optimal.cost)
                    {
                       
                        // // Optimal Node found
                        optimal.cost = nodeInProcess.cost;
                        for (map<int, string> ::iterator it = nodeInProcess.setOfStringsInMap.begin (); it != nodeInProcess.setOfStringsInMap.end (); it++)
                        {
                            optimal.setOfStringsInMap[it->first] = it->second;                            
                        }
                    }
                    nodeQueue.pop();
                    flag = true;
                    break;
                }

                map<int, string>::iterator it = nodeInProcess.setOfStringsInMap.find (randomNumberArray[i]);

                if (it == nodeInProcess.setOfStringsInMap.end ())
                {
                    // Generation of New Node considering all strings at once

                    int c=0,n=nodeInProcess.setOfStringsInMap.size();
                    string stringSet[n];
                    getStringsForThisNode(nodeInProcess,stringSet);
                    string stringInConsideration=setOfStrings.at(randomNumberArray[i]);
                    string pat=modifiedEditDistance(stringInConsideration,stringSet,&c,n);
                    Node temp;

					string stringSetResult[n];

                    for (int k = 0; k < n; ++k)
                    {
                        stringSetResult[k]="";
                    }

                    stringInConsideration = handleEditingInNewEditDistance(stringInConsideration,stringSet,n,pat,stringSetResult);

                    temp.setOfStringsInMap[randomNumberArray[i]]=stringInConsideration;

                    int j=0;
                    for (std::map<int, string>::iterator itInner = nodeInProcess.setOfStringsInMap.begin (); itInner != nodeInProcess.setOfStringsInMap.end (); ++itInner){
                        temp.setOfStringsInMap[itInner->first]=stringSetResult[j]; 
                        j++;          
                    }
                    temp.cost=getMeCostOfThisNode(temp);
                    nodeQueue.push(temp);
                }
                nodeQueue.pop();
            }       
        }
    }

}
开发者ID:kapilthakkar72,项目名称:String-Mapping,代码行数:85,代码来源:AIPart2.cpp

示例5: getdis

double getdis(int stx, int sty, int enx, int eny) {
	pid ex;
	pq.push(make_pair(0, make_pair(stx, sty)));

	int i;
	int j;
	
	if (board[stx][sty] == 1) {
		return 0;
	}

	int test;

	while (!pq.empty()) {
		ex = pq.top();
		pq.pop();
		mp1.insert(make_pair(ex.second, 1));
		mp.erase(ex.second);
		i = ex.second.first;
		j = ex.second.second;

		if ((ex.second.first == enx) && (ex.second.second == eny)) {
			return ex.first;
		}
	
		if ((j + 1 < m) && (board[i][j + 1] == 0) && (mp1.count(make_pair(i, j+1)) == 0)) {
			if (mp.count(make_pair(i, j + 1)) == 0) {
				pq.push(make_pair(ex.first + 1, make_pair(i, j+1)));
				mp.insert(make_pair(make_pair(i,j + 1),ex.first + 1));
			}
			else {
				test = mp[make_pair(i, j + 1)];
				if (test > ex.first + 1) {
					pq.push(make_pair(ex.first + 1, make_pair(i, j+1)));
					mp[make_pair(i,j+1)] = ex.first + 1;
				}
			}
		}

		if (((j + 1 < m) && (i + 1 < n)) && (board[i + 1][j + 1] == 0) && (mp1.count(make_pair(i + 1, j+1)) == 0)) {
			if (mp.count(make_pair(i + 1, j + 1)) == 0) {
				pq.push(make_pair(ex.first + rt2, make_pair(i + 1, j+1)));
				mp.insert(make_pair(make_pair(i + 1,j + 1),ex.first + rt2));
			}
			else {
				test = mp[make_pair(i + 1, j + 1)];
				if (test > ex.first + rt2) {
					pq.push(make_pair(ex.first + rt2, make_pair(i+1, j+1)));
					mp[make_pair(i+1,j+1)] = ex.first + rt2;
				}
			}
		}

		if ((i + 1 < n) && (board[i+1][j] == 0) && (mp1.count(make_pair(i + 1, j)) == 0)) {
			if (mp.count(make_pair(i + 1, j)) == 0) {
				pq.push(make_pair(ex.first + 1, make_pair(i + 1, j)));
				mp.insert(make_pair(make_pair(i + 1,j),ex.first + 1));
			}
			else {
				test = mp[make_pair(i + 1, j)];
				if (test > ex.first + 1) {
					pq.push(make_pair(ex.first + 1, make_pair(i + 1, j)));
					mp[make_pair(i+1,j)] = ex.first + 1;
				}
			}
		}

		if (((j - 1 >= 0) && (i + 1 < n)) && (board[i + 1][j - 1] == 0) && (mp1.count(make_pair(i + 1, j-1)) == 0)) {
			if (mp.count(make_pair(i + 1, j - 1)) == 0) {
				pq.push(make_pair(ex.first + rt2, make_pair(i + 1, j-1)));
				mp.insert(make_pair(make_pair(i + 1,j - 1),ex.first + rt2));
			}
			else {
				test = mp[make_pair(i + 1, j - 1)];
				if (test > ex.first + rt2) {
					pq.push(make_pair(ex.first + rt2, make_pair(i+1, j-1)));
					mp[make_pair(i+1,j-1)] = ex.first + rt2;
				}
			}
		}

		if ((j - 1 >= 0) && (board[i][j - 1] == 0) && (mp1.count(make_pair(i, j-1)) == 0)) {
			if (mp.count(make_pair(i, j - 1)) == 0) {
				pq.push(make_pair(ex.first + 1, make_pair(i, j - 1)));
				mp.insert(make_pair(make_pair(i,j - 1),ex.first + 1));
			}
			else {
				test = mp[make_pair(i, j - 1)];
				if (test > ex.first + 1) {
					pq.push(make_pair(ex.first + 1, make_pair(i, j - 1)));
					mp[make_pair(i,j-1)] = ex.first + 1;
				}
			}
		}

		if (((j - 1 >= 0) && (i - 1 >= 0)) && (board[i - 1][j - 1] == 0) && (mp1.count(make_pair(i-1, j-1)) == 0)) {
			if (mp.count(make_pair(i - 1, j - 1)) == 0) {
				pq.push(make_pair(ex.first + rt2, make_pair(i - 1, j-1)));
				mp.insert(make_pair(make_pair(i - 1,j - 1),ex.first + rt2));
			}
//.........这里部分代码省略.........
开发者ID:shadowwalkers,项目名称:Artificial_Intelligence_codes,代码行数:101,代码来源:q35.cpp

示例6: compresss

void compresss(FILE * in, const char * targetFilename)
{
    init();
    unsigned char c;
    
    while(fscanf(in,"%c",&c) != EOF)
    {
        charInfo[c].num++;
        charInfo[c].value = int(c);
        //printf("%c ",c);
    }
    for(int i=0; i < Maxn; i++)
    {
        if(charInfo[i].num)
        {
            //printf(":::%c ",charInfo[i].value);
            charInfo[i].position = nodeNum++;
            charPrioriytQueue.push(charInfo[i]);
        }
    }
    
    sumCharNum = nodeNum;
    while(charPrioriytQueue.size() > 1)
    {
        struct Info first, second;
        
        first = charPrioriytQueue.top();
        charPrioriytQueue.pop();
        second = charPrioriytQueue.top();
        charPrioriytQueue.pop();
        
        struct Info next;
        next.num = first.num + second.num;
        next.position = nodeNum++;
        
        child[next.position][0] = first.position;
        child[next.position][1] = second.position;
        parent[first.position][0] = next.position;
        parent[second.position][0] = next.position;
        parent[first.position][1] = 0; //from left
        parent[second.position][1] = 1; //from right
        
        charPrioriytQueue.push(next);
    }
    rootNodeNum = nodeNum-1;
    FILE * out = fopen(targetFilename, "w+");
    writeCompressInfo(out);
    
    fseek(in, 0, SEEK_SET);
    
    int current = 0, digitNum = 0;
    //string compressString = "";
    while(fscanf(in, "%c", &c) != EOF)
    {
        //printf("%c",c);
        int leafPosition = charInfo[c].position;
        string temp = "";
        while(leafPosition != rootNodeNum)
        {
            if(parent[leafPosition][1])
            {
                temp += "1";
            }
            else
            {
                temp += "0";
            }
            leafPosition = parent[leafPosition][0];
        }
        for(int i=temp.length()-1; i>=0; i--)
        {
            digitNum++;
            if(temp[i]=='0')
                current = current<<1;
            else
                current = (current<<1)+1;
            if(digitNum == 8)
            {
                digitNum = 0;
                //printf("%d:%d",current,compressString.length());
                fputc(current, out);
                //stringstream ss;
                //ss<<(unsigned char)current;
                //string tt = ss.str();
                //compressString = compressString + tt;
                current = 0;
            }
        }
    }
    if(digitNum)
    {
        //printf("%d\n",current);
        //stringstream ss;
        //ss<<(unsigned char)current;
        //string tt = ss.str();
        //compressString = compressString + tt;
        //printf("digit::%d\n", digitNum);
        fputc(current, out);
        //fprintf(out, "%s", compressString.c_str());
        string temp = CompressedFlag;
//.........这里部分代码省略.........
开发者ID:Accagain2014,项目名称:cip,代码行数:101,代码来源:main.cpp

示例7: Bfs

void Bfs(){
    int v,tt,tf,nextk;
    dp[1][0][0][T] = R;
    Q.push(node(1,0,0,T));
    inq[1][0][0][T] = 1;
    while(!Q.empty()){
        node x = Q.top();
        Q.pop();
        inq[x.a][x.b][x.c][x.d] = 0;
        int tmp = dp[x.a][x.b][x.c][x.d];
        nextk = (x.c + 1) % K;
        for(int i = first[x.a]; i != -1; i = next[i]){
            v = ver[i];
            tt = tim[i];
            tf = fee[i];
            if(x.d >= tt && tmp >= tf){ //能去下一点
                if(v == 1 && x.c != 0)
                    continue;
                if(v == N){
                    if(x.c == 0)
                        ans = max(ans,tmp - tf);
                }
                else{
                    if(tmp - tf > dp[v][x.b][x.c][x.d - tt]){
                        //去下点,不行动
                        dp[v][x.b][x.c][x.d - tt] = tmp - tf;
                        if(!inq[v][x.b][x.c][x.d - tt]){
                            Q.push(node(v,x.b,x.c,x.d - tt));
                            inq[v][x.b][x.c][x.d - tt] = 1;
                        }
                    }
                    if(v != 1&& x.b < B  &&tmp - tf - pri[x.c][v] > dp[v][x.b + 1][x.c][x.d - tt]){
                        //去下点,买
                        dp[v][x.b + 1][x.c][x.d - tt] = tmp - tf - pri[x.c][v];
                        if(!inq[v][x.b + 1][x.c][x.d - tt]){
                            Q.push(node(v,x.b + 1,x.c,x.d - tt));
                            inq[v][x.b + 1][x.c][x.d - tt] = 1;
                        }
                    }
                    if(v != 1 && x.b > 0 && tmp - tf + pri[x.c][v] > dp[v][x.b - 1][x.c][x.d - tt]){
                        //去下点,卖
                        dp[v][x.b - 1][x.c][x.d - tt] = tmp - tf + pri[x.c][v];
                        if(!inq[v][x.b - 1][x.c][x.d - tt]){
                            Q.push(node(v,x.b - 1,x.c,x.d - tt));
                            inq[v][x.b - 1][x.c][x.d - tt] = 1;
                        }
                    }
                }
            }
        }
        if(x.d > 0 && x.a != 1 ){
            if(tmp >dp[x.a][x.b][nextk][x.d - 1]){
                //去下个宇宙,不行动
                dp[x.a][x.b][nextk][x.d - 1] = tmp;
                if(!inq[x.a][x.b][nextk][x.d - 1]){
                    Q.push(node(x.a,x.b,nextk,x.d - 1));
                    inq[x.a][x.b][nextk][x.d - 1] = 1;
                }
            }
            if(x.b < B &&  tmp - pri[nextk][x.a] > dp[x.a][x.b + 1][nextk][x.d - 1]){
                //去下个宇宙,买
                dp[x.a][x.b + 1][nextk][x.d - 1] = tmp - pri[nextk][x.a];
                if(!inq[x.a][x.b + 1][nextk][x.d - 1]){
                    Q.push(node(x.a,x.b + 1,nextk,x.d - 1));
                    inq[x.a][x.b + 1][nextk][x.d - 1] = 1;
                }
            }
            if(x.b > 0 &&  tmp + pri[nextk][x.a] > dp[x.a][x.b - 1][nextk][x.d - 1]){
                //去下个宇宙,卖
                dp[x.a][x.b - 1][nextk][x.d - 1] = tmp + pri[nextk][x.a];
                if(!inq[x.a][x.b - 1][nextk][x.d - 1]){
                    Q.push(node(x.a,x.b - 1,nextk,x.d - 1));
                    inq[x.a][x.b - 1][nextk][x.d - 1] = 1;
                }
            }
        }
    }
}
开发者ID:Natureal,项目名称:HDU,代码行数:78,代码来源:47843.cpp

示例8: Solve

void Solve()
{
	while (!que.empty())
	{
		que.pop();
	}
	int i, j;
	for (i = 0; i < 1200; i++)
	{
		for (j = 0; j < 20; j++)
		{
			w[i][j] = INF;
		}
	}
	WF x, y;
	ACM *pAcm;
	x.nStatus = (1 << 1);
	x.pos = 1;
	x.val = 0;
	w[2][1] = 0;
	que.push(x);
	while (!que.empty())
	{
		x = que.top();
		que.pop();
		if (x.val > w[x.nStatus][x.pos])
		{
			continue;
		}
		pAcm = pList[x.pos];
		while (pAcm != NULL)
		{
			y.nStatus = (x.nStatus | (1 << pAcm->v));
			y.pos = pAcm->v;
			if ((x.nStatus | (1 << pAcm->c)) == x.nStatus)
			{
				y.val = x.val + pAcm->p;
			}
			else
			{
				y.val = x.val + pAcm->r;
			}
			if (y.val < w[y.nStatus][y.pos])
			{
				w[y.nStatus][y.pos] = y.val;
				que.push(y);
			}
			pAcm = pAcm->next;
		}
	}
	int ans = INF;
	for (i = 1; i <= 1024; i++)
	{
		if (w[i][n] < ans)
		{
			ans = w[i][n];
		}
	}
	if (ans == INF)
	{
		printf("impossible\n");
		return;
	}
	printf("%d\n", ans);
}
开发者ID:fengrenchang86,项目名称:PKU,代码行数:65,代码来源:5935874_RE.cpp

示例9: main

int main() {
    int i, u, v,  sz, nodes, edges, starting;
 	double w;
 	
    // create graph
    while(scanf("%d", &nodes)){
    if(nodes==0)
    	return 0;
    scanf("%d", &edges);
    //cout<<nodes<<" "<<edges<<"\n";
    priority_queue< pii, vector< pii >, comp > Q;
	vector< pii > G[MAX];

    for(int i=0;i<=nodes;i++)
    {
    	F[i]=0;
    }
    
    for(i=0; i<edges; i++) {
        scanf("%d %d %lf", &u, &v, &w);
        w = w/100;
        //cout<<w<<" \n";
        G[u].pb(pii(v, w));
        G[v].pb(pii(u, w)); // for undirected
    }
    
   /* 
    for(int i=1;i<=nodes;i++)
    {
    	for(int j=0;j<G[i].size();j++)
    	{
    		cout<<G[i].at(j).first<<" "<<G[i].at(j).second<<" ";
    	}
    	cout<<endl;
    }
    
    */
    
    //scanf("%d", &starting);
 	starting  = 1;
    // initialize distance vector
    for(i=1; i<=nodes; i++) D[i] = 0;
    D[starting] = 1;
    Q.push(pii(starting, 1));
 
    // dijkstra
    while(!Q.empty()) {
        u = Q.top().first;
        Q.pop();
        if(F[u]) continue;
        sz = G[u].size();
        for(i=0; i<sz; i++) {
            v = G[u][i].first;
            w = G[u][i].second;
            if(!F[v] && D[u]*w > D[v]) {
                D[v] = D[u]*w;
                Q.push(pii(v, D[v]));
            }
        }
        F[u] = 1; // done with u
    }
 
    // result
    //for(i=1; i<=nodes; i++) printf("Node %d, min weight = %d\n", i, D[i]);
    double ans = D[nodes] * 100;
    printf("%.6lf percent\n",ans);
    
    
    
    }
    return 0;
}
开发者ID:vishesh49,项目名称:master,代码行数:72,代码来源:CHICAGO.cpp

示例10: main

int main()
{
	int N,T,j,i;
	int SP[25];
	bool f=false;
	LAKE lakes[26];

	while (cin>>N,N)
	{
		cin>>T;
		T*=12;
		for (i=0;i<N;++i)
		{
			cin>>lakes[i].CUR;
			lakes[i].id=i;
		}
		for (i=0;i<N;++i)
		{
			cin>>lakes[i].DEC;
		}
		for (i=0;i<N-1;++i)
		{
			cin>>SP[i];
		}
		int sum=0,maxsum=-1,fishnum;
		int usesumtime=0;
		int TIME[26]={0},maxTime[26]={0};
		for (i=0;i<N;++i)	//most far lake id
		{
			while (!PQ.empty())
			{
				PQ.pop();
			}
			for (j=0;j<=i;++j)
			{
				if (lakes[j].CUR>0)
				PQ.push(lakes[j]);
				TIME[j]=0;
			}
			int lefttime=T-sum;
			fishnum=0;
			while (lefttime-- && !PQ.empty())
			{
				LAKE l=PQ.top();
				PQ.pop();
				fishnum+=l.CUR;
				TIME[l.id]+=5;
				l.CUR-=l.DEC;
				if (l.CUR>0)
				{
					PQ.push(l);
				}
			}
			//TIME[0]+=lefttime*5+5;
			usesumtime=0;
			if (fishnum>maxsum)
			{
				maxsum=fishnum;
				for (j=0;j<=i;++j)
				{
					maxTime[j]=TIME[j];
					usesumtime+=TIME[j];
				}
				maxTime[0]+=((T-sum)*5-usesumtime);
			}
			if ((sum+=SP[i])>=T)
			{
				break;
			}
		}
		if (f)
		{
			cout<<endl;
		}
		f=true;
		for (j=0;j<N-1;++j)
		{
			cout<<maxTime[j]<<", ";
		}
		cout<<maxTime[j]<<endl;
		cout<<"Number of fish expected: "<<maxsum<<"\n";
	}
}
开发者ID:dingyaguang117,项目名称:AOJ,代码行数:83,代码来源:main.cpp

示例11: setAlarm

void setAlarm(long Time, int n){
	request r = request(Time,n);
	if((r.Time < MaxTime)&&(r.Time>=Time)){
		timeHeap.push(r);
	}
}
开发者ID:abhaymitra,项目名称:social-network-simulator,代码行数:6,代码来源:timekeeper.cpp

示例12: main

int main(){
	int M,N;
	cin >> M >> N;
	while(M != -1 || N != -1){
		memset(xe,0,sizeof(xe));
		memset(ye,0,sizeof(ye));
		int x,y,d,t;
		int mx,my;
		mx = my = 0;
		for(int i = 0;i < M;i++){	//初始化墙
			cin >> x >> y >> d >> t;
			if(d == 1){
				for(int j = 0;j < t;j++){
					ye[x][y + j] = INFINITY;	//ye[x][y]表示 x y+1这条边;
				}
				mx = max(mx,x);
				my = max(my,t+y);
			}
			else{
				for(int j = 0;j < t;j++){
					xe[x + j][y] = INFINITY;	//xe[x][y]表示 x+1 y这条边;
				}
				mx = max(mx,x);
				my = max(my,t+y);	
			}
		}
		for(int i = 0;i < N;i++){	//初始化门
			cin >> x >> y >> d;
			if(d == 1){
				ye[x][y + 1] = 1;
				mx = max(mx,x);
				my = max(my,1+y);
			}
			else{
				xe[x + 1][y] = 1;
				mx = max(mx,x+1);
				my = max(my,y);
			}
		}
		float xNemo,yNemo;
		cin >> xNemo >> yNemo;
		if(xNemo > mx || yNemo > my){	//Nemo不在墙内,直接输出0
			cout << "0" << endl;
			continue;
		}
		f_x = int(xNemo);
		f_y = int(yNemo);
		for(int i = 0;i <= mx;i++){
			for(int j = 0;j <= my;j++){
				dis[i][j] = INFINITY;
			}
		}
		dis[0][0] = 0;
		int temp_x,temp_y;
		int flag = 0;
		while(!q.empty()){
			q.pop();
		}
		q.push(Node(0,0,0));
		while(!q.empty()){	//从0 0出发bfs直到到达目的地
			temp_x = q.top().x;
			temp_y = q.top().y;
			q.pop();
			if(temp_x == f_x && temp_y == f_y){		//到达目的地,退出
				flag = 1;
				break;
			}
			if(temp_y+1<=my && dis[temp_x][temp_y+1]>dis[temp_x][temp_y]+xe[temp_x][temp_y+1]){		//向上走
				dis[temp_x][temp_y+1] = dis[temp_x][temp_y]+xe[temp_x][temp_y+1];
				q.push(Node(temp_x,temp_y+1,dis[temp_x][temp_y+1]));
			}
			if(temp_x+1<=mx && dis[temp_x+1][temp_y]>dis[temp_x][temp_y]+ye[temp_x+1][temp_y]){		//向右走
				dis[temp_x+1][temp_y] = dis[temp_x][temp_y]+ye[temp_x+1][temp_y];
				q.push(Node(temp_x+1,temp_y,dis[temp_x+1][temp_y]));
			}
			if(temp_y-1>=0 && dis[temp_x][temp_y-1]>dis[temp_x][temp_y]+xe[temp_x][temp_y]){		//向下走,重要
				dis[temp_x][temp_y-1] = dis[temp_x][temp_y]+xe[temp_x][temp_y];	//不是+xe[tmp_x][tmp_y-1]
				q.push(Node(temp_x,temp_y-1,dis[temp_x][temp_y-1]));
			}
			if(temp_x-1>=0 && dis[temp_x-1][temp_y]>dis[temp_x][temp_y]+ye[temp_x][temp_y]){	//向左走,注意同上
				dis[temp_x-1][temp_y] = dis[temp_x][temp_y]+ye[temp_x][temp_y];
				q.push(Node(temp_x-1,temp_y,dis[temp_x-1][temp_y]));
			}
		}
		if(flag){
			cout << dis[f_x][f_y] << endl;
		}
		else{
			cout << "-1" << endl;
		}
	}
	return 0;
}
开发者ID:hellozts4120,项目名称:Algorithm,代码行数:93,代码来源:Finding+Nemo.cpp

示例13: main1057stack

int main1057stack()
{
	int count;
	cin >> count;
	char* inputstr = new char[20];
	while (count-- > 0)
	{
		scanf("%s",inputstr);
		if (strcmp(inputstr, "PeekMedian")==0)
		{
			if (sta.empty())
			{
				printf("Invalid\n");
			}
			else
			{
				int temp;
				int median = (pq.size()+1)/2;
				while (--median)
				{
					temp = pq.top();
					stacktemp.push(temp);
					pq.pop();
				}
				printf("%d\n", pq.top());
				while (!stacktemp.empty())
				{
					temp = stacktemp.top();
					stacktemp.pop();
					pq.push(temp);
				}
			}
		}
		else if (strcmp(inputstr, "Pop") == 0)
		{
			int num,temp;
			if (sta.empty())
			{
				printf("Invalid\n");
			}
			else
			{
				num = sta.top(); 
				sta.pop();
				printf("%d\n",num);
				while (num != pq.top())
				{
					temp = pq.top();
					stacktemp.push(temp);
					pq.pop();
				}
				pq.pop();
				while (!stacktemp.empty())
				{
					temp = stacktemp.top();
					stacktemp.pop();
					pq.push(temp);
				}
			}
		}
		else if (strcmp(inputstr, "Push") == 0)
		{
			int num;
			scanf("%d", &num);
			sta.push(num);

			pq.push(num);
		}
		else
		{
			assert(false);
		}
	}
	return 0;
}
开发者ID:dickyhui,项目名称:CODE,代码行数:75,代码来源:pat_1057_Stack.cpp

示例14: init

void init()
{
	int i,j,k;
	scanf("%d",&M);

	while( !heap.empty() ) heap.pop();

	//初始化
	for (i=1;i<=N;++i) 
	{
		dis[i]=INFINITE;
		visit[i]=false;
		path[i]=0;
		bian[i][0]=0;
		for (j=1;j<=N;++j) map[i][j]=INFINITE;
	}

	//读入信息
	for (k=1;k<=M;++k)
	{
		scanf("%d%d",&i,&j);
		scanf("%d",&map[i][j]);
		map[j][i]=map[i][j];

		bian[i][0]++;
		bian[j][0]++;
		bian[i][bian[i][0]]=j;
		bian[j][bian[j][0]]=i;
	}

	//堆初始化
	dis[2]=0;
	path[2]=1;
	node temp;
	temp.dist=0;
	temp.ID=2;
	heap.push(temp);

	int l;
	while (true)
	{
		//从堆中找出最小距离
		while (!heap.empty() && visit[heap.top().ID]) heap.pop();
		if (heap.empty()) break;

		k=heap.top().ID;
		dis[k]=heap.top().dist;
		heap.pop();

		visit[k]=true;
		for (j=1;j<=bian[k][0];++j) 
		{
			l=bian[k][j];
			//满足松弛操作,加入堆
			if (dis[k]+map[k][l]<dis[l]) 
			{
				dis[l]=dis[k]+map[k][l];
				temp.ID=l;
				temp.dist=dis[l];
				heap.push(temp);
			}
			//DP计算方案
			if (visit[l] && dis[l]<dis[k]) path[k]+=path[l];
		}

	}

	printf("%d\n",path[1]);
}
开发者ID:springer126,项目名称:C-World,代码行数:69,代码来源:1036.cpp

示例15: upt

	void upt(int i, int v) {
		if(v<p[i]) {
			p[i]=v;
			que.push(MP(-p[i], i));
		}
	}
开发者ID:caiwaifung,项目名称:algo,代码行数:6,代码来源:EmoticonsDiv1.cpp


注:本文中的priority_queue::push方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。