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;
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
 void insert(int key, int value) {
     exists[key] = true;
     current[key] = value - delta;
     heap.push(make_pair(-current[key], key));
 }
Ejemplo n.º 4
0
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();
            }       
        }
    }

}
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));
			}
			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 >= 0) && (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 < m) && (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));
			}
			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;
				}
			}
		}


	}
}
Ejemplo n.º 6
0
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;
        fseek(out, temp.length(), SEEK_SET);
        fprintf(out, "%d", digitNum);
    }
    else
    {
        string temp = CompressedFlag;
        fseek(out, temp.length(), SEEK_SET);
        fprintf(out, "8");
    }
    
    printf("Compress successful done.\n");
    fclose(out);
    fclose(in);
    
    return ;
}
Ejemplo n.º 7
0
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;
                }
            }
        }
    }
}
Ejemplo n.º 8
0
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);
}
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;
}
Ejemplo n.º 10
0
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";
	}
}
void setAlarm(long Time, int n){
	request r = request(Time,n);
	if((r.Time < MaxTime)&&(r.Time>=Time)){
		timeHeap.push(r);
	}
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
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;
}
Ejemplo n.º 14
0
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]);
}
Ejemplo n.º 15
0
	void upt(int i, int v) {
		if(v<p[i]) {
			p[i]=v;
			que.push(MP(-p[i], i));
		}
	}
Ejemplo n.º 16
0
pii solve(int N){
  if (N == 2){
    int c = 1;
    for (auto v : s){
      set_col(v,c);
      c++;
    }
    return MP(1,2);
  }
  int f1 = get_top();
  int v1 = leafSon[f1].back();
  leafSon[f1].pop_back();
  int f2 = get_top();
  while (f1 == f2)
    f2 = get_top();
  int v2 = leafSon[f2].back();
  leafSon[f2].pop_back();
  
  s.erase(v1); s.erase(v2);
  for (auto it : E[v1]) deg[it]--;
  for (auto it : E[v2]) deg[it]--;
  for (auto it : E[v1]){
    if (s.count(it) && deg[it] == 1){
      add_son(it);
    }
  }
  for (auto it : E[v2]){
    if (s.count(it) && deg[it] == 1){
      add_son(it);
    }
  }
  if (leafSon[f1].size()){
    pq.push(MP(deg[f1], f1));
  }
  if (leafSon[f2].size()){
    pq.push(MP(deg[f2], f2));
  }
  pii tmp = solve(N-2);
  int c = N/2 + 1;
  if (col[f1] != col[f2]){
    set_col(v1, c);
    set_col(v2, c);
    return tmp;
  } else {
    if (tmp.F == tmp.S){
      set_col(v1, tmp.F);
      set_col(v2, c);
      return MP(tmp.S, c);
    } else {
      int u = c2v[tmp.F][0];
      if (E[u].count(f2)){
        swap(f1,f2);
        swap(v1,v2);
      }
      if (E[u].count(f1)){
        set_col(v1, c);
        swap_col(v1, f1);
        set_col(v2, tmp.F);
      } else {
        set_col(v1, tmp.F);
        set_col(v2, c);
      }
      return MP(tmp.S, c);
    }
  }
}
Ejemplo n.º 17
0
int main()
{
	scanf("%d", &t);
	while(t--)
	{
		tot = 2;
		scanf("%lf%d%lf%lf%lf%lf", &r, &n, &pp[0].x, &pp[0].y, &pp[1].x, &pp[1].y);
		for(int i = 0; i < n; ++i)
			scanf("%lf%lf", &p[i].x, &p[i].y);
		for(int i = 0; i < n; ++i)
			for(int j = i + 1; j < n; ++j)
			{
				Point tmp = p[i] - p[j];
				if(dcmp(tmp.mode() - 2 * r) > 0 || !dcmp(tmp.mode()))
					continue;
				Point rev = (Point){tmp.y, -tmp.x}, oo = (p[i] + p[j]) * 0.5;
				double dis = sqrt(r * r - (tmp.x * tmp.x + tmp.y * tmp.y) / 4);
				rev = rev * (dis / rev.mode());
				Point t1 = oo + rev, t2 = oo - rev;
				if(valid(t1))
					pp[tot++] = t1;
				if(dcmp(rev.mode()) && valid(t2))
					pp[tot++] = t2;
			}
		for(int i = 0; i < tot; ++i)
			e[i].clear();
		for(int i = 0; i < tot; ++i)
			for(int j = i + 1; j < tot; ++j)
				if(cover(pp[i], pp[j]))
				{
					e[i].push_back(make_pair((pp[i] - pp[j]).mode(), j));
					e[j].push_back(make_pair((pp[i] - pp[j]).mode(), i));
				}
		memset(vis, 0, sizeof vis);
		for(int i = 0; i < tot; ++i)
			dist[i] = INF;
		while(!Q.empty())
			Q.pop();
		dist[0] = 0;
		Q.push(make_pair(dist[0], 0));
		while(!Q.empty())
		{
			int u = Q.top().second;
			Q.pop();
			if(vis[u])
				continue;
			vis[u] = 1;
			for(vector<edge>::iterator it = e[u].begin(); it != e[u].end(); ++it)
			{
				int &v = it -> second;
				double &w = it -> first;
				if(dcmp(dist[v] - dist[u] - w) > 0)
				{
					dist[v] = dist[u] + w;
					Q.push(make_pair(dist[v], v));
				}
			}
		}
		if(!dcmp(dist[1] - INF))
			puts("-1");
		else
			printf("%.8f\n", dist[1]);
	}
	return 0;
}
void MSTSort(int* list, int rank, int size, int localV, int V, int MD) {

    //head node array for each process
    int headNodeArray[V+1]; //the last element of head node array tells the processes to continue or are they all done (0 == done)
    int localEdgeCount = 0;
    edge fakeEdge;
    //construct MST = array of edges of size V - 1
    vector<edge> MST;

    //process 0
    if (rank == 0) {

        //construct head node array for p0
        for (int x = 0; x < V; x++) {
            //each vertex points to itself initially
            headNodeArray[x] = x;
        } headNodeArray[V] = 1; //flag for continuing

    }//end if rank 0

    //local matrix for each proc's vertices it gets
    int localArraySize = localV * (MD * 2);
    int localList[localArraySize];

    //scatter the data into the local array(s)
    MPI_Scatter(list, localArraySize, MPI_INT, localList, localArraySize, MPI_INT, 0, MPI_COMM_WORLD);

    //broadcast the head node array
    MPI_Bcast(headNodeArray, V, MPI_INT, 0, MPI_COMM_WORLD);

    //MPI_Barrier(MPI_COMM_WORLD);

    //PRINT LOCAL list (only non zero edges for each process)
    if( PRINTING && DETAILED){
      cout << "\nPROCESS " << rank <<
            ":: localArraySize::" << localArraySize <<
            ":: localV::" << localV;
      for (int x = 0; x < localArraySize; x+=2) {
        if (x % (MD*2) == 0 ) cout << "\n";
        if (localList[x+1] != 0){
            cout << localV * rank + (x / (MD*2))
                 << "->" << localList[x] << ":" << localList[x+1] << "   "; }
        } cout << "\n\n";
    }//end print     */

    //priority q for each process' edges
    priority_queue<edge, vector<edge>, compareEdgeWeights> edgepq;

    int x = 0; //array offset
    //for each vertex locally owned
    for (int i = 0; i < localArraySize; i += MD*2){
        x = i; //jump to vertex i data in local array
        //while non zero weight from array
        while (localList[x+1] != 0){

          edge temp;
          temp.v1 = (localV * rank + (x / (MD*2))); //vertex 1
          temp.v2 = localList[x];
          temp.weight = localList[x+1];
          temp.use = 2;
          edgepq.push(temp);

          x+=2;
          localEdgeCount++;
        }
    }

    //while processor has more edges
    //while(! edgepq.empty() ){
    while(headNodeArray[V] != 0){
      edge least;
      fakeEdge.v1 = 0; fakeEdge.v2 = 0; fakeEdge.weight = INT_MAX;  //processor is done with valid edges

      if (edgepq.empty()){
        least = fakeEdge;
      }else{
        //find least weight, usable edge
        least = edgepq.top();
        while( !check_edge(least, headNodeArray, false) && !edgepq.empty()){
            edgepq.pop();
            if( edgepq.empty() ) {
              least = fakeEdge;
            }else {
              least = edgepq.top();
            }
        }
      }
        //temp printing of found least edges
        //if(PRINTING) cout << "p" << rank<< " found weight " << least.weight <<" "<< least.v1 << "->"<< least.v2 << endl;

        //send least edge to p0
        if(rank != 0){
            //SEND EDGE
            pass_change(least);
        } else if(rank == 0){
            //receive edges, and passes it's own leasst weight edge as well
            if( !dist_change(size, headNodeArray, least, MST)){
                headNodeArray[V] = 0; //all done flag
            }
        }

        //remove edge
        if (!edgepq.empty())
          edgepq.pop();
        localEdgeCount--;

        //update new Head node array to procs
        MPI_Bcast(headNodeArray, V+1, MPI_INT, 0, MPI_COMM_WORLD);
    }

    //print the final MST
    if (PRINTING && rank == 0)
	  {
		    printMST(MST);
	  }
    else if(!PRINTING && rank == 0)
    {
    	ofstream outfile;
    	outfile.open("mst.txt");
    	  for (int ab = 0; ab < MST.size(); ab++)
    	    outfile << "MST Edge from v" << MST[ab].v1 << " to v" << MST[ab].v2
    	          << ", weight "<<MST[ab].weight << "\n";
        outfile << "MST size =" << MST.size();
    }


}//end MSTSort
Ejemplo n.º 19
0
string startSearch()
{
    Node current;
    int hn,tCost,aCost;
    char name;
    vector<int> vertices;
    vector<int>::iterator it,it3;
    vector<int> nextUnvisitedCities;
    vector<char> namesOfCitiesYettoVisit,namesOfCitiesYettoVisit2;
    vector<char>::iterator it2;
    string pathSoFar,state,npathSoFar,nstate;
    string res;
    while(!astar.empty() and astar.top().totalCost < optimumCost)
    {
        current=astar.top();
         astar.pop();
        //cout<<"current is "<<current.citynum<<" with cost "<<current.totalCost<<"\n";
        //cout<<"size of priority_queue is "<<astar.size()<<"\n";
        pathSoFar=current.pathSoFar;
        state=current.state;
        
        //cout<<"the vertices are \n";
        
        vertices=current.citiesLeft;
        for(it=vertices.begin();it!=vertices.end();it++)
        {
            int next=*it;
           // cout<<"vertice  is "<<next<<"\n";
         }
        // the state is the entire path + in the closed list with a lesser cost then no need to expand this node at all
        // test for goal state
        if(current.citiesNotVisited==0)
        {
            //i.e. all cities have been visited, just the tour back to the starting city is left.
            int tcost=current.actualCost + distance(current.citynum,0);
            cout<<" goal state reached with cost of "<<tcost<<" and with a path of "<<current.pathSoFar<<"\n";
            if(tcost< optimumCost )
            {
                optimumCost=tcost;
                res=current.pathSoFar;

            }
           
                     continue;
        }
        if(closedList.find(state)!=closedList.end() and closedList[state] < current.totalCost)
        {
            
            continue;
        }
       
        //cout<<"vertices.size is "<<vertices.size()<<"\n";
        
        for(it=vertices.begin();it!=vertices.end();it++)
        {
            int next=*it;
            //cout<<"next is "<<next<<"\n";
            nextUnvisitedCities.clear();
            namesOfCitiesYettoVisit.clear();
   


            //cout<<"calling hn for next= "<<next<<" and vertices are\n";
            vector<int>:: iterator it4;
            //nextUnvisitedCities.push_back(123);
            for(it3=vertices.begin();it3!=vertices.end();it3++)
            {
                int x=*it3;
                
                if(x==next)
                {
                    //cout<<"x==next\n";
                    continue;
                } 
                //cout<<"*it3 is "<<x<<" ";
                nextUnvisitedCities.push_back(x);
            }
            //for(it4=nextUnvisitedCities.begin();it4!=nextUnvisitedCities.end();it4++)
            //cout<<*it4<<" ";
            //cout<<"\n";
            for(it4=vertices.begin();it4!=vertices.end();it4++)
            {
                int city3=*it4;
                if(city3==next) continue;
                if( city3>25)
                {
                    namesOfCitiesYettoVisit.push_back('a'+ city3-26);
                }
                else
                {
                    namesOfCitiesYettoVisit.push_back('A'+city3);
                }
            }
            hn=calculateHeuristic(nextUnvisitedCities,namesOfCitiesYettoVisit,next);//this will return the 
            //cout<<"hn for "<<next<<" is "<<hn<<"\n";
            aCost=current.actualCost + distance(current.citynum,next);
            tCost=hn+ aCost;
            //cout<<"totalCost for next= "<<next<<" is "<<tCost<<"\n";
            if( next>25)
            {
                name='a'+ next-26;
            }
            else
            {
                name='A'+next;
            }
            npathSoFar=pathSoFar+name;
            string temps=npathSoFar;
            sort(temps.begin(),temps.end());
            nstate=temps+name;
            //City[nodeTrack++]=createNode(next,npathSoFar,current.citiesNotVisited-1,name,hn,aCost,tCost,nstate,nextUnvisitedCities);
            //cout<<"pushing next= "<<next<<" current.citiesNotVisited-1 ="<<current.citiesNotVisited-1<<"\n";
            astar.push(createNode(next,npathSoFar,current.citiesNotVisited-1,name,hn,aCost,tCost,nstate,nextUnvisitedCities));
            
            
        }

    }
    return res;
}
Ejemplo n.º 20
0
int main()
{
	int i;
	st tmp, ta, tb;
	st *root;

	while(gets(s) != NULL && strcmp(s, "END")){
		len = strlen(s);
		memset(c, 0, 256 * sizeof(int));
		memset(cl, 0, 256 * sizeof(int));
		cc = 0;
		for(i = 0; i < len; ++i){
			if(!c[s[i]]){
				++cc;
			}
			++c[s[i]];
		}
		if(cc == 1){
			printf("%d %d %.1f\n", 8 * len, len, 8.0);
			continue;
		}
		for(i = 0; i < 256; ++i){
			if(c[i] > 0){
				tmp.c = c[i];
				tmp.ch = i;
				tmp.left = NULL;
				tmp.right = NULL;
				pq.push(tmp);
			}
		}
		/*
		while(!pq.empty()){
			tmp = pq.top();
			pq.pop();
			printf("%d\n", tmp.c);
		}
		continue;
		*/
		for(i = 0; i < cc - 1; ++i){
			ta = pq.top();
			pq.pop();
			tb = pq.top();
			pq.pop();
			tmp = st();
			tmp.c = ta.c + tb.c;
			tmp.ch = 0;
			tmp.left = new st(ta);
			tmp.right = new st(tb);
			pq.push(tmp);
		}
		root = &(pq.top());
		get_depth(root, 0);
		clear_tree(root);
		while(!pq.empty()){
			pq.pop();
		}

		int sum = 0;
		for(i = 0; i < len; ++i){
			sum += cl[s[i]];
		}

		printf("%d %d %.1f\n", 8 * len, sum, 8.0 * len / sum);
	}

	return 0;
}
Ejemplo n.º 21
0
 void add(pqNode t){
     pq.push(t);
 }
int main()  
{  
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

	init();

	int res = game(h, t);

	if (res == 0)
	{
		puts("Draw");
		return 0;
	}

	if (res == 1) puts("Ivan");
	if (res == 2) puts("Zmey");
	
	if (res == 2)
	{
		int res2 = rec(h, t, res);
		printf("%d\n", res2);
	} else {
		q.push(make(0, h, t));
		dp[h][t] = 0;
		while (!q.empty())
		{
			piii cur = q.top();
			q.pop();
			int x = cur.second.first;
			int y = cur.second.second;

			if (x + y > R) continue;

			int w = cur.first;
			if (dp[x][y] != w) continue;
			
			int lim = min(x, n);
			for (int i = 1; i <= lim; ++i)
			{
				int dx = H[i].first;
				int dy = H[i].second;
				int nx = x - i + dx;
				int ny = y + dy;
				
				if (dp[nx][ny] == -1 || dp[nx][ny] > dp[x][y] + 1)
				{
					dp[nx][ny] = dp[x][y] + 1;
					q.push(make(dp[nx][ny], nx, ny));
				}
			}

			lim = min(y, m);
			for (int i = 1; i <= lim; ++i)
			{
				int dx = T[i].first;
				int dy = T[i].second;
				int nx = x + dx;
				int ny = y - i + dy;
				
				if (dp[nx][ny] == -1 || dp[nx][ny] > dp[x][y] + 1)
				{
					dp[nx][ny] = dp[x][y] + 1;
					q.push(make(dp[nx][ny], nx, ny));
				}
			}
		}

		printf("%d\n", dp[0][0]);
	}
    return 0;  
}
void PQPeelTriangles()
{

    TRIANGLEDENSITY= 3.0*NumTriangles/V; 
	TrianglePeelSize=V;
	TrianglePeelSizeFraction=1.0;
	TrianglePeelFe = 2*E/(V*(V-1));
	TrianglePeelTe = 6*NumTriangles/(V*(V-1)*(V-2));
	TrianglePeelEdgeden = 2*E/V;
	
	double numedges=(double)E;
	double numoftriangles=NumTriangles;
	double numvertices=(double)V;	
	
	
	
	for (int i = 1; i <= V; ++i) 
        q.push(make_pair(triangles[i], i));

	int c = q.top().second;
	int counter = 0; 
	while (!q.empty()) 
	{
		int c = q.top().second;
        q.pop();
         if (triangles[c] < 0 )// == -1) 
		{ 
            continue;
        }
		else
		{ 
			TriangleOptVals[counter] = 3.0*numoftriangles/numvertices ;
			permutation[counter++]=c; 
		}
		
        numedges -= degrees[c]; // number of edges goes down by degree of c
        --numvertices; // one vertex less now 
		numoftriangles -= triangles[c];
        triangles[c] = -1; //no need to look at it again 
		if (numvertices > 0) 
		{
			if( TRIANGLEDENSITY <= 3.0*numoftriangles/numvertices  ) 
			{
				TrianglePeelEdgeden = 2.0*numedges/numvertices;
				TRIANGLEDENSITY = 3.0*numoftriangles/numvertices ;
				TrianglePeelSize = numvertices;
				TrianglePeelSizeFraction = numvertices/V;
				TrianglePeelFe = 2*numedges/(numvertices*(numvertices-1));
				TrianglePeelTe = 6*numoftriangles/(numvertices*(numvertices-1)*(numvertices-2));
				OPTIND = counter; 
				//subgraph.clear();
			}
        } 
		for (int i=0; i < AdjList[c].size();i++)
		{
			int w = AdjList[c][i];
			degrees[w]--;
			int Tw = triangles[w];
			if( Tw >= 0 && degrees[c] >=2 ) 
			{
			for(int j = 0; j < AdjList[w].size(); j++)
			{	
  				    int candidate = AdjList[w][j];					
					if( triangles[candidate] > 0 && candidate !=c)
					{
					    
						if( degrees[c] <= degrees[candidate] )
						{
							for (int rc=0; rc < AdjList[c].size(); rc++)
							{
							  if( AdjList[c][rc] == candidate)
									triangles[w]--; 		  								 						
							}
						}
						else
						{
							for (int rc=0; rc < AdjList[candidate].size(); rc++)
								if( AdjList[candidate][rc] == c)									
											triangles[w]--; 		
						}
						
					}				
					
				}
				
				q.push(make_pair(triangles[w], w));				
			}	
			 
		}
		if( counter == V )
		    break;
	}

}
void dij(node s,int c){
	
	int i,j,k,l;
	node temp,temp1;
	
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			for(k=1;k<=tim;k++){
				d[k][c][i][j]=inf;
			}
		}
	}
	
	i=s.x; j=s.y; 
	s.lev=1;
	d[1][c][i][j]=0; 
	s.cost=0;
	pq.push(s);
	
	while(!pq.empty()){
		
		temp=pq.top(); pq.pop();
		i=temp.x; j=temp.y;
		
		if(temp.lev==tim) continue;
		k=i-1; l=j;
		
		/*
		if(k>0&&l>0&&k<=n&&l<=n&&d[temp.lev+1][c][k][l]>temp.cost+mat[k][l]){
			d[temp.lev+1][c][k][l]=temp.cost+mat[k][l];
			temp1.lev=temp.lev+1;
			temp1.cost=d[temp.lev+1][c][k][l]; 
			
		}
		*/
		if(valid) {
			d[temp.lev+1][c][k][l]=temp.cost+mat[i][j];
         	temp1.lev=temp.lev+1;
			temp1.cost=d[temp.lev+1][c][k][l];
			temp1.x=k; temp1.y=l;
			pq.push(temp1); 
		}
		
		k=i+1; l=j;
		if(valid){
			d[temp.lev+1][c][k][l]=temp.cost+mat[i][j];
         	temp1.lev=temp.lev+1;
			temp1.cost=d[temp.lev+1][c][k][l];
			temp1.x=k; temp1.y=l;
			pq.push(temp1); 
		}
		
		k=i;l=j-1;
		
		if(valid){
			d[temp.lev+1][c][k][l]=temp.cost+mat[i][j];
         	temp1.lev=temp.lev+1;
			temp1.cost=d[temp.lev+1][c][k][l];
			temp1.x=k; temp1.y=l;
			pq.push(temp1); 
		}
		
		k=i;l=j+1;
		
		if(valid){
			
			d[temp.lev+1][c][k][l]=temp.cost+mat[i][j];
		   	temp1.lev=temp.lev+1;
			temp1.cost=d[temp.lev+1][c][k][l];
		
		//	printf("k-%d %d %d\n",k,l,temp1.cost);
			
			temp1.x=k; temp1.y=l;
			pq.push(temp1); 
		
		}
		
						
		
	}
	
	/*
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++){
			for(k=1;k<=tim;k++){
				printf("%d %d %d %d %d\n",i,j,k,c,d[k][c][i][j]);
			}
			printf("\n");
		}
		printf("\n");
	}
	*/
	/*
	for(k=1;k<=tim;k++){
		printf("c - %d \n",d[k][c][1][4]);
	}
	printf("\n");
	*/
 }
Ejemplo n.º 25
0
int main(){

//    freopen("~/in","r",stdin);

    scanf("%d %d",&n,&m);
    scanf("%d %d",&x,&y);


    for (int i=0;i<m;i++){
        scanf("%d %d %d",&etmp.st,&etmp.ed,&etmp.length);
        edges[etmp.st].push_back(etmp);
        swap(etmp.st,etmp.ed);
        edges[etmp.st].push_back(etmp);
    }
    for (int i=1;i<=n;i++){
        scanf("%d %d",&dl[i],&ct[i]);
    }



    while(!pq.empty()){
        pq.pop();
    }
    memset(visted,0,sizeof(int)*(n+1));
    memset(shortest,0,sizeof(int)*(n+1));
    memset(sed,0,sizeof(sed));
    shortest[x] = -1;
    ntmp.value = 0;
    ntmp.id = x;
    pq.push(ntmp);
    bool flag = false;
    while(!pq.empty()){
        ntmp = pq.top();
        pq.pop();
        if (ntmp.id==y){
            printf("%I64d\n",ntmp.value);
            flag = true;
            break;
        }
        if (sed[ntmp.id] == true)  continue;
        sed[ntmp.id] = true;

        //扩展路径

        memset(visted,0,sizeof(int)*(n+1));
        roads.clear();
        visted[ntmp.id] = 2000000000;
        spread(ntmp.id,ntmp.id,dl[ntmp.id]);
        itro = roads.begin();
        for (;itro!=roads.end();itro++){
            new_node.id = itro->dest;
            if (new_node.id==ntmp.id) continue;
            new_node.value = itro->cost+ntmp.value;
            if (shortest[new_node.id]==0||new_node.value<shortest[new_node.id]){
                shortest[new_node.id] = new_node.value;
                pq.push(new_node);
            }
        }

    }
    if (flag==false)
        printf("-1\n");
    return 0;
}
Ejemplo n.º 26
0
int main()
{
	int say=0;
	memset(mark,-1,sizeof(mark));
	memset(dp,-1,sizeof(dp));
	scanf("%d %d",&n,&m);
	for (int i = 0; i < m; i++)
	{
		int o1,o2,o3;
		scanf("%d %d %d",&o1,&o2,&o3);
		yol[o1].pb(MP(o2,o3));
		yol[o2].pb(MP(o1,o3));
		if(o3) say++;
		if(o3)
		{
			mp[MP(o1,o2)]=1;
			mp[MP(o2,o1)]=1;		
		}
	}
	q.push(MP(0,1));
	while(!q.empty())
	{
		ii tmp=q.top();
		q.pop();
		if(mark[tmp.sc]!=-1) continue;
		mark[tmp.sc]=-tmp.fi;
		for (int i = 0; i < (int)yol[tmp.sc].size(); i++)
		if(mark[yol[tmp.sc][i].fi]==-1)
		{
			ii yeni=MP(tmp.fi-1,yol[tmp.sc][i].fi);
			q.push(yeni);
		}
	}
	cout <<say-2*fon(1)+mark[n]<<endl;
	yaz(1);
	int yy[N][3],cevs=0;
	for (int i = 0; i <(int) prt.size()-1; i++)
	{
		if(mp[MP(prt[i],prt[i+1])])
		{
			mp[MP(prt[i],prt[i+1])]=0;
			mp[MP(prt[i+1],prt[i])]=0;
		}
		else
		{
			yy[cevs][0]=prt[i];
			yy[cevs][1]=prt[i+1];
			yy[cevs][2]=1;
			cevs++;
		}
	}
  std::map<ii,int>::iterator it = mp.begin();
	for (it=mp.begin(); it!=mp.end(); ++it)
		if(it->sc && (it->first).fi > (it->first).sc)
		{

			yy[cevs][0]=it->first.fi;
			yy[cevs][1]=it->first.sc;
			yy[cevs][2]=0;
			cevs++;
		}
	for (int i = 0; i < cevs; i++)
	{
		printf("%d %d %d\n",yy[i][0],yy[i][1],yy[i][2]);
	}
	
}
Ejemplo n.º 27
0
void process(int vtx) {    // so, we use -ve sign to reverse the sort order
  taken[vtx] = 1;
  for (int j = 0; j < (int)AdjList[vtx].size(); j++) {
    ii v = AdjList[vtx][j];
    if (!taken[v.first]) pq.push(ii(-v.second, -v.first));
} }                            
Ejemplo n.º 28
0
int main()
{
	freopen("secure.in", "r", stdin);
	freopen("secure.out", "w", stdout);

	cin >> n >> m;

	for (int i = 1; i <= n; i++)
		cin >> a[i];
			
	for (int i = 0; i < m; i++) {
		cin >> s >> t >> c;

		g[s].push_back(t);
		w[s].push_back(c);

		g[t].push_back(s);
		w[t].push_back(c);
	} 

	for (int i = 1; i <= n; i++) {
		if (a[i] == 1) {
			q.push(make_pair(0ll, i));
			p[i] = i;
		} else {
			d[i] = inf;
		}
		if (a[i] == 2)
			need[i] = true;
	} 

	while (!q.empty()) {
		cur = - q.top().first;
		v = q.top().second;

		q.pop();

		if (cur > d[v])
			continue;

		for (int i = 0; i < (int)g[v].size(); i++) {
			to = g[v][i];

			if (d[to] > d[v] + w[v][i]) {
				p[to] = p[v];
				d[to] = d[v] + w[v][i];
				q.push(make_pair(- d[to], to));
			}
		} 
	}

	LL pos = - 1;

	for (int i = 1; i <= n; i++)
		if (need[i] && (pos == - 1 || d[pos] > d[i]))
			pos = i;

	if (pos == - 1 || d[pos] == inf)
		cout << - 1;
	else
		cout << p[pos] << ' ' << pos << ' ' << d[pos];

	return 0;
}
Ejemplo n.º 29
0
	REP(i, n) {
		if (!vst[i]) {
			long long cost = dfs(i) / 2;
			region.push({-cost, i});
		}
	}
Ejemplo n.º 30
0
void update(int pos)
{
	que.push(make_pair(tot[pos],pos));
}