ESJAIL – Escape from Jail SPOJ (Time Limit Exceeded)

I am trying to solve this problem (ESJAIL – Escape from Jail) from SPOJ, but I am getting TLE and cannot figure out why. Please, can someone tell me where is the bug?

I did a BFS approach. During the BFS I save the nodes, that I can´t reach, in a vector (dts). Then, as soon as the queue is empty, I put the nodes that I saved previously in it, so I can see if I now can pass through them. The process ends when the number of visited nodes remains the same (which means that no more nodes can be visited, thus no nodes can be unlocked).

#include<iostream>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;
int main()
{
    int n,k,c;
    while(true)
    {
        scanf("%d%d%d",&n,&k,&c);
        if(n==-1&&k==-1&c==-1)break;
        vector<vector<int> > g(n+1,vector<int>());
        vector<int> keys(n+1,0);
        vector<int> dts;
        vector<bool> v(n+1,false);
        queue<int> q;
        q.push(1);
        v[0]=true;
        for(int i=0;i<k;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            keys[b]=a;
        }
        for(int i=0;i<c;i++)
        {
            int c,d;
            scanf("%d%d",&c,&d);
            g[c].push_back(d);
            g[d].push_back(c);
        }
        int numvis=0;
        int lastnv=0;
        int time=0;
        while(true)
        {
            if(time>n*c)
            {
                printf("N/n");
                break;
            }
            if(!q.empty())
            {
                int x=q.front();
                q.pop();
                if(v[keys[x]])
                {
                    v[x]=true;
                    if(x==n)
                    {
                        printf("Y\n");
                        break;
                    }
                    numvis++;
                    for(int j=0;j<g[x].size();j++)
                    {
                        if(!v[g[x][j]])
                        {
                            q.push(g[x][j]);
                        }
                    }
                }
                else
                {
                    dts.push_back(x);
                }
            }
            else
            {
                if(dts.empty())break;
                int l=dts.size();
                for(int i=0;i<l;i++)
                {
                    q.push(dts[i]);
                    v[dts[i]]=false;
                }
                dts.clear();
                if(lastnv==numvis)
                {
                    break;
                }
                else
                {
                    lastnv=numvis;
                }
            }
            time++;
        }
        if(!v[n])printf("N\n");
    }
}

read more

ESJAIL – Escape from Jail SPOJ (Time Limit Exceeded)

I am trying to solve this problem (ESJAIL – Escape from Jail) from SPOJ, but I am getting TLE and cannot figure out why. Please, can someone tell me where is the bug?

I did a BFS approach. During the BFS I save the nodes, that I can´t reach, in a vector (dts). Then, as soon as the queue is empty, I put the nodes that I saved previously in it, so I can see if I now can pass through them. The process ends when the number of visited nodes remains the same (which means that no more nodes can be visited, thus no nodes can be unlocked).

#include<iostream>
#include<vector>
#include<queue>
#include<fstream>
using namespace std;
int main()
{
    int n,k,c;
    while(true)
    {
        scanf("%d%d%d",&n,&k,&c);
        if(n==-1&&k==-1&c==-1)break;
        vector<vector<int> > g(n+1,vector<int>());
        vector<int> keys(n+1,0);
        vector<int> dts;
        vector<bool> v(n+1,false);
        queue<int> q;
        q.push(1);
        v[0]=true;
        for(int i=0;i<k;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            keys[b]=a;
        }
        for(int i=0;i<c;i++)
        {
            int c,d;
            scanf("%d%d",&c,&d);
            g[c].push_back(d);
            g[d].push_back(c);
        }
        int numvis=0;
        int lastnv=0;
        int time=0;
        while(true)
        {
            if(time>n*c)
            {
                printf("N/n");
                break;
            }
            if(!q.empty())
            {
                int x=q.front();
                q.pop();
                if(v[keys[x]])
                {
                    v[x]=true;
                    if(x==n)
                    {
                        printf("Y\n");
                        break;
                    }
                    numvis++;
                    for(int j=0;j<g[x].size();j++)
                    {
                        if(!v[g[x][j]])
                        {
                            q.push(g[x][j]);
                        }
                    }
                }
                else
                {
                    dts.push_back(x);
                }
            }
            else
            {
                if(dts.empty())break;
                int l=dts.size();
                for(int i=0;i<l;i++)
                {
                    q.push(dts[i]);
                    v[dts[i]]=false;
                }
                dts.clear();
                if(lastnv==numvis)
                {
                    break;
                }
                else
                {
                    lastnv=numvis;
                }
            }
            time++;
        }
        if(!v[n])printf("N\n");
    }
}

read more

each listitem to open different activity

the code has listview and click on each item pops up alertdialog . alertdialog has two radio buttons “yes” ” no ” and then an “ok” button . when selected “yes” it takes to next activity , if selected “no” it stays in same activity . my problem is no matter which listitem I choose , i am taken to same ‘Next-activity’ . I want it to be taken to different activities depending on listitem chosen.

     listview = (ListView) findViewById(R.id.mylistview);
     final String[] items = new String[]{"IOS", "ANDROID", "WINDOWS"};
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items);
     listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int itemposition = position;

            String itemvalue = (String) listview.getItemAtPosition(position);
            final CharSequence[] items1 = {"yes", "no"};
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("slection confirmation");

            builder.setCancelable(false);
            builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selection = (String) items1[which];
                    Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();

                }
            });
            builder.setNeutralButton("OK",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (selection)
                    {
                        case("yes"):
                            Intent myint=new Intent(MainActivity.this,secondpage.class);
                            myint.putExtra("act1","");
                            startActivity(myint);
                            break;
                        case("no"):
                            dialog.cancel();
                            break;
                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

}

read more

each listitem to open different activity

the code has listview and click on each item pops up alertdialog . alertdialog has two radio buttons “yes” ” no ” and then an “ok” button . when selected “yes” it takes to next activity , if selected “no” it stays in same activity . my problem is no matter which listitem I choose , i am taken to same ‘Next-activity’ . I want it to be taken to different activities depending on listitem chosen.

     listview = (ListView) findViewById(R.id.mylistview);
     final String[] items = new String[]{"IOS", "ANDROID", "WINDOWS"};
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items);
     listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int itemposition = position;

            String itemvalue = (String) listview.getItemAtPosition(position);
            final CharSequence[] items1 = {"yes", "no"};
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("slection confirmation");

            builder.setCancelable(false);
            builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selection = (String) items1[which];
                    Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();

                }
            });
            builder.setNeutralButton("OK",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (selection)
                    {
                        case("yes"):
                            Intent myint=new Intent(MainActivity.this,secondpage.class);
                            myint.putExtra("act1","");
                            startActivity(myint);
                            break;
                        case("no"):
                            dialog.cancel();
                            break;
                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

}

read more