viewing paste Unknown #52433 | Text

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<queue>
#include<chrono>
#include<map>
using namespace std::chrono;
using namespace std;
const int maxpoint = 1e+7;
const int maxedge = 1e+6;
const int inf = 99999999;
 
struct e
{
    int v, next;//无权值,指向的点,下一条边的地址
}edge[maxedge];
vector<int> pa;
string property[maxpoint];
bool isroot[maxpoint], visit[maxpoint];
int head[maxpoint], dep[maxpoint], chaxun[20];//查询数组
int hcnt, N;//N为查询属性的个数
vector<int> dis[maxpoint];
vector<int> split(const string& str, const string& pattern)
{
    vector<int> res;
    if (str == "")
        return res;
    //在字符串末尾也加入分隔符,方便截取最后一段
    string strs = str + pattern;
    size_t pos = strs.find(pattern);
 
    while (pos != strs.npos)
    {
        string temp = strs.substr(0, pos);
        if (temp == "")
            return res;
        res.push_back(stoi(temp));
        //去掉已分割的字符串,在剩下的字符串中进行分割
        strs = strs.substr(pos + 1, strs.size());
        pos = strs.find(pattern);
    }
    return res;
}//字符串分割所用到的
void init() {
    hcnt = 0;
    memset(isroot, 0, sizeof isroot);
    memset(head, -1, sizeof head);
    memset(dep, -1, sizeof dep);
}
void addedge(int u, int v)
{
    edge[hcnt].v = v;
    edge[hcnt].next = head[u];
    head[u] = hcnt++;
}
 
void bfs(int p,vector<int> pro) {
    memset(visit, 0, sizeof visit);
    queue<int> Q;
    Q.push(p);
    dep[p] = 0;
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
        if (!visit[u])
        {
            if (isroot[u])
            {
                if (dis[u].size() < N)
                {
                    dis[u].resize(N);
                    for (int i = 0; i < N; i++)
                        dis[u][i] = inf;
                }
                for(int i = 0;i < N ; i++)
                    for (int k = 0; k < pro.size(); k++)
                        if (pro[k] == chaxun[i] && dep[u] < dis[u][i])
                                dis[u][i] = dep[u];//更新标签
            }
            for (int i = head[u]; i != -1; i = edge[i].next)
            {
                int v = edge[i].v;
                if (!visit[v])
                {
                    dep[v] = dep[u] + 1;
                    Q.push(v);
 
                }
            }
            visit[u] = 1;
        }
        
    }
}
void bfsentry() {
    
    for (int i = 0; i < maxpoint; i++)
    {
        vector<int> temp = split(property[i],",");
        int flag = 0;
        for(int k=0;k<N;k++)
            for (int j = 0; j < temp.size(); j++)
                if (temp[j] == chaxun[k])
                {
                    flag = 1;
                    break;
                }
        if (flag == 1)//如果该点拥有查询属性
        {
            bfs(i, temp);
        }
        
    }
}
int isparent(int p1, int p2)
{
    int t1 = 0, t2 = 0, equ = 0;
    for (int i = 0; i < N; i++)
    {
        if (dis[p1][i] > dis[p2][i])
            t1++;
        if (dis[p1][i] > dis[p2][i])
            t2++;
        else {
            equ++;
            t1++;
            t2++;
        }
    }
    if (t1 == N && equ != N)
        return 1;
    if (t2 == N && equ != N)
        return -1;
    else  return 0;
}
void findparent()
{
    pa.clear();
    for (int p = 0; p < maxpoint; p++)
    {
 
        if (dis[p].size() == N) {
            int flag = 1;
            for (int i = 0; i < N; i++)
                if (dis[p][i] == inf)
                {
                    flag = 0;
                    break;
                }
            if (flag==1) {
                if (pa.size() == 0)
                {
                    pa.push_back(p);
                    continue;
                }
                int opflag = 0, firstflag = 1;
                vector<int>::iterator it = pa.begin();
                for (; it != pa.end(); it++)
                {
                    int temp = isparent(p, *it);
                    if (temp == 1)
                    {
                        if (firstflag == 1)
                        {
                            *it = p;
                            opflag = 0;
                        }
                        else {
                            pa.erase(it);
                            it--;
                        }
                        opflag = 1;
                    }
                    else if (temp == -1)
                    {
                        opflag = 1;
                        break;
                    }
                }
                if (opflag == 0)//如果都没有支配或者被支配,加入新的祖宗集
                    pa.push_back(p);
            }
        }
    }
    }
void processpoint(string o) {
    int index = o.find_first_of(" ");
    int srcpoint = stoi(o.substr(0, index));
    property[srcpoint] = o.substr(index + 1, o.length());
}
void processroot(string o) {
    int index = o.find_first_of(" ");
    int srcpoint = stoi(o.substr(0, index));
    isroot[srcpoint] = true;
}
void processedge(string o)
{
    int index = o.find_first_of(" ");
    int srcpoint = stoi(o.substr(0, index));
    string edg = o.substr(index + 1, o.length());
    vector<int> pointto = split(edg, ",");
    for (int i = 0; i < pointto.size(); i++)
        addedge(srcpoint, pointto[i]);
    
}
void getedge(string path) {
    fstream file(path);//读取文件
    if (file.is_open())
    {
        while (!file.eof())
        {
            string got;
            getline(file, got);
            if (!got.empty())
            {
                processedge(got);
            }
        }
    }
}
void gettext(string path)
{
    fstream file(path);//读取文件
    if (file.is_open())
    {
        while (!file.eof())
        {
            string got;
            getline(file, got);
            if (!got.empty())
            {
                processpoint(got);
            }
        }
    }
}
void getroot(string path)
{
    fstream file(path);//读取文件
    if (file.is_open())
    {
        while (!file.eof())
        {
            string got;
            getline(file, got);
            if (!got.empty())
            {
                processroot(got);
            }
        }
    }
 
}
int main()
{
    cout << "正在初始化" << endl;
    init();
    cout << "开始构造边" << endl;
    auto t1 = steady_clock::now();
    getedge("G:/作业/算法设计作业/大作业/Yago_small/edge.txt");
    cout << "开始构造属性" << endl;
    gettext("G:/作业/算法设计作业/大作业/Yago_small/node_keywords.txt");
    cout << "开始构造根节点数组" << endl;
    getroot("G:/作业/算法设计作业/大作业/placeid2coordYagoVB.txt");
    auto t2 = steady_clock::now();
    auto time1 = (t2 - t1).count() / 1e6;
    cout << "构造用时" << time1 << "ms" << endl;
    cout << "请输入查询属性的个数(不大于20个):";
    cin >> N;
    cout << "请输入查询属性(数字),以空格分开(不大于20个):";
    for (int i = 0; i < N; i++)
        cin >> chaxun[i];
 
    cout << "开始对每个根节点进行bfs" << endl;
    t1 = steady_clock::now();
    bfsentry();
    cout << "语义地点构造完毕,开始在语义地点寻找skyline" << endl;
    findparent();
    t2 = steady_clock::now();
    time1 = (t2 - t1).count() / 1e6;
    cout << "寻找完毕,用时" << time1 << "ms,是skyline的点有" << pa.size() << "个,分别是:" << endl;
 
    for (int i = 0; i < pa.size(); i++)
        cout << pa[i] << " ";
    system("pause");
}
Viewed 520 times, submitted by Guest.