Ejemplo n.º 1
0
int main() {
    int tot_case = 0;
    while (scanf("%d%d%d", &c, &s, &q) && (c || s || q)) {
        // Input.
        graph.Init(c);
        for (int i = 0; i < s; i++) {
            int c1, c2, d;
            scanf("%d%d%d", &c1, &c2, &d);
            c1--, c2--;
            graph.AddEdge(c1, c2, d);
            graph.AddEdge(c2, c1, d);
        }
        // Solve.
        Floyd::Go(graph.mat, c);
        // Output.
        printf("%s", tot_case ? "\n" : "");
        printf("Case #%d\n", ++tot_case);
        for (int i = 0; i < q; i++) {
            int c1, c2, d;
            scanf("%d%d", &c1, &c2);
            c1--, c2--;
            if (graph.mat[c1][c2] != INF) {
                printf("%d\n", graph.mat[c1][c2]);
            } else {
                printf("no path\n");
            }
        }
    }
    return 0;
}
/*将状态向量转换成对应图g_target*/
void FromG2G(Graph& g_original,Graph& g_target,int* lu)
{
	g_target.Init();
	g_target.nV = g_original.nV;  
	g_target.nId = g_original.nId;                                                /*图的编号可以不用*/
	for(int u = 1; u <= g_original.nV; u++)
	{
		for(int v = 1; v <= g_original.nV; v++)
		{
			if((g_original.matrix[u][v].iC > 0) //边
				&& (lu[g_original.matrix[u][v].iLabel] == 1))
			{
				g_target.matrix[u][v].dP = g_original.matrix[u][v].dP;
				g_target.matrix[u][v].iC = g_original.matrix[u][v].iC;
				g_target.matrix[u][v].iLabel = g_original.matrix[u][v].iLabel;
				g_target.nE++;  
			}
		}
	}
}
Ejemplo n.º 3
0
int main() {
    int tot_case = 0;
    while (scanf("%d%d", &n, &r) && (n || r)) {
        // Input.
        graph.Init(n);
        for (int i = 0; i < r; i++) {
            scanf("%d%d%d", &c1, &c2, &p);
            graph.AddEdge(c1 - 1, c2 - 1, p);
            graph.AddEdge(c2 - 1, c1 - 1, p);
        }
        scanf("%d%d%d", &s, &d, &t);
        // Solve.
        Dijkstra::Go(dis, graph.mat, n, s - 1);
        // Output.
        printf("Scenario #%d\n", ++tot_case);
        printf("Minimum Number of Trips = %d\n", (t + dis[d - 1] - 2) / (dis[d - 1] - 1));
        printf("\n");
    }
    return 0;
}
Ejemplo n.º 4
0
int main()
{
	Graph g;	// graph 

	while(1)
	{
		cerr << "Graph > ";
		string command;
		cin >> command;

		if(command.compare("quit") == 0)
		{
			break;
		}
		else if(command.compare("vertices") == 0)
		{
			int nn;
			cin >> nn;
			g.Init(nn);
			cout << "New graph : " << nn << " nodes" << endl;
		}
		else if(command.compare("edge") == 0)
Ejemplo n.º 5
0
int main() {
    int tot_case = 0;
    while (scanf("%d", &n) != EOF) {
        // Input.
        graph.Init(N);
        Read(0, n);
        for (int i = 1; i < N - 1; i++) {
            scanf("%d", &n);
            Read(i, n);
        }
        // Solve.
        Floyd::Go(graph.mat, N);
        // Output.
        printf("Test Set #%d\n", ++tot_case);
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            printf("%2d to %2d: %d\n", a, b, graph.mat[a - 1][b - 1]);
        }
        printf("\n");
    }
    return 0;
}
int _tmain(int argc, char* argv[])
{
	char stFileName[BUFFER_SIZE]     = WORK_SPACE;             /*源点汇点*/
	char fileName[BUFFER_SIZE]       = WORK_SPACE;             /*数据存放文件*/
	char resultFileName[BUFFER_SIZE] = WORK_SPACE;             /*实验结果存放文件*/
	/*
	if (4 != argc)
	{
		cout<<"Command Params : "<<endl
			<<"\tSource_Sink File Name"<<endl
			<<"\tGraph data File Name"<<endl
			<<"\tResult File Name"<<endl;
		return 1;
	}
	strcat_s(stFileName,argv[1]);
	strcat_s(fileName,argv[2]);
	strcat_s(resultFileName,argv[3]);
	*/
	char argv1[BUFFER_SIZE]="data\\st\\V8E14s-t_4.txt";
	char argv2[BUFFER_SIZE]="data\\graph\\V8E14_4.txt";
	char argv3[BUFFER_SIZE]="data\\results\\V8E14_4.txt";
	strcat_s(stFileName,argv1);
	strcat_s(fileName,argv2);
	strcat_s(resultFileName,argv3);
	
	cout<<stFileName<<endl<<fileName<<endl<<resultFileName<<endl;
	
	/*filestream to write information in the file*/
	ofstream out_result;
	out_result.open(resultFileName,ios::out|ios::app);         /*保存实验结果*/
	if(!out_result.is_open())
	{
		printf("open result file failed...\n");
		exit(1);
	}

	HANDLE hProcess;                                          /*用于测进程占用的内存*/
	PROCESS_MEMORY_COUNTERS pmc;
	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION 
		| PROCESS_VM_READ,FALSE,GetCurrentProcessId());
	if (NULL == hProcess)
    {
        cout << "Process Hanle Error !" << endl;
		out_result.close();                                    /*防止资源泄露*/
		return -1;
    }

	double dP = 0.0;
	double timeCost = 0.0;
	__int64 start = 0;                                         /*用于测量时间(精确到1ms)*/
	__int64 frequency = 0;                                     /*与机器平台相关*/
	__int64 counter = 0;
	SIZE_T memsize;

	QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); 

	InputReader inReader(fileName,stFileName);	
	int s,t;
	Flow maxPmaxF;
	int maxflow;
	Graph g;
	g.Init();

	while(inReader.ReadGraph(g))                               /*读取文件中的图*/
	{
		inReader.ReadSourceSink(s,t);
		/*读一个图数据处理一个图*/

		QueryPerformanceCounter((LARGE_INTEGER*)&start);      /*记录开始时间*/
		dP = GetMPMF(g,s,t,maxflow,maxPmaxF,&StateMtrix);     /*运行核心算法*/
		 
		//核心算法
		computeICA(&StateMtrix, &key_edge_set, g, s, t);


		QueryPerformanceCounter((LARGE_INTEGER*)&counter);    /*记录结束时间*/
		timeCost = (counter - start) / double(frequency)*1000;/*返回单位是毫秒*/
		GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc));   /*获得进程使用的内存使用情况*/
		memsize = pmc.WorkingSetSize;                         /*获得进程消耗的内存*/

		/*将运行结果输出到文件*/
		PrintFmax_Prob(out_result,s,t,maxflow,dP);            /*保存最可靠最大流分布概率到结果文件*/
		out_result<<"状态划分算法消耗的内存为:"              /*输出内存使用情况到结果文件*/
			<<(double)memsize/MB<<endl;
		PrintTime(out_result,timeCost);                       /*保存运行时间到结果文件*/
		PrintFlow(out_result,maxPmaxF,g.nV);                  /*输出最可靠的最大流分布到结果文件*/	
		/*将关键边输出*/
		printKeyEdge(out_result,key_edge_set);
		g.Init();
		/*初始化*/
		init_KeyEdgeSet(key_edge_set,StateMtrix);
		
	}

	CloseHandle(hProcess);                                    /*关闭进程句柄*/
	out_result.close();                                       /*关闭结果文件*/
	//暂停
	//getchar();
	return 0;
}