In view of the ever-increasing distributed generation in the urban power grid, appropriate power arrangement of distributed generation is presented to optimize active power flow of the urban power grid. By considering different effects of various kinds of distributed generation on environment, the environment value index of contamination is used to evaluate the effect on environment by contamination. A model is developed for environment cost including the charge for drainage and cost of environment pollution prevention. Then a model for OPF including environment cost is developed with the penalty function method. The primal dual interior point method is used to solve this OPF model. Finally, simulation test is carried out on the data of an urban power grid. The results show that with the environment cost, production cost, and grid loss taken into account, the cost and grid loss can be reduced and environment contamination mitigated at the same time.